Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-29-2013, 04:02 PM   PM User | #1
mangisqa
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
mangisqa is an unknown quantity at this point
Date Format Help

Good Morning:

I am sure there is a simply solution but I am relatively new at this. I have the following code that works fine except I need help reformatting the date:

Code:
<script>
var curr = new Date(); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();

document.write("Information is for week ending "+ lastday)
</script>
It returns the following:
Information is for week ending Sat, 2 Feb 2013 15:59:49 UTC

I would like it it to read:
Information is for week ending 02/02/2013

Thank you in advance for your help!
mangisqa is offline   Reply With Quote
Old 01-29-2013, 04:22 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
One of may possible ways.
Code:
<script>
Number.prototype.padDigit = function() { return (this < 10) ? '0'+this : this; }
var curr = new Date(); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = new Date(curr.setDate(first));
var lastday = new Date(curr.setDate(last));
var lday = (lastday.getMonth()+1).padDigit()
         + '/'+lastday.getDate().padDigit()
         + '/'+(lastday.getFullYear()-2000).padDigit();
document.write("Information is for week ending "+ lday)
</script>
Remove the -2000 in the FullYear() caluclation for 4 digit year.
Good Luck!

Last edited by jmrker; 01-29-2013 at 04:33 PM.. Reason: Finally typed faster than 'Philip M'. Yeah !!!
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
mangisqa (01-29-2013)
Old 01-29-2013, 04:26 PM   PM User | #3
mangisqa
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
mangisqa is an unknown quantity at this point
Excellent- worked like a charm!
mangisqa is offline   Reply With Quote
Old 01-29-2013, 04:30 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are:-

Code:
<script type = "text/javascript">
var curr = new Date(); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var lastday = new Date(curr.setDate(last));
var yy = lastday.getFullYear();
var mth = lastday.getMonth()+1;
if (mth<10) {mth = "0" + mth}
var dy = lastday.getDate();
if (dy<10) {dy = "0" + dy}

// now manipulate the display of year,month,date as desired
var result = dy + "/" + mth + "/" + yy;  // UK format - change for USA format if required
document.write("Information is for week ending "+ result)
</script>
Be aware that document.write() has been obsolete since Netscape 3 passed away 10+ years ago. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

The people who followed the Lord were called the 12 Decibels.
- Pupil's answer to Catholic Elementary School test.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 01-29-2013 at 04:39 PM..
Philip M is offline   Reply With Quote
Old 01-29-2013, 04:33 PM   PM User | #5
niralsoni
Regular Coder

 
Join Date: Mar 2008
Location: London
Posts: 129
Thanks: 1
Thanked 31 Times in 31 Posts
niralsoni is an unknown quantity at this point
With reference to following -
1) http://www.codeproject.com/Articles/...ts-with-Protot
2) http://www.codeproject.com/Articles/...pt-Date-Format

Here is your solution -

Code:
String.prototype.times = function(n)
{
 var s = '';
 for (var i = 0; i < n; i++)
  s += this;

 return s;
}
// Zero-Padding
String.prototype.zp = function(n) { return '0'.times(n - this.length) + this; }

// a global month names array
var gsMonthNames = new Array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);
// a global day names array
var gsDayNames = new Array(
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
);
// the date format prototype
Date.prototype.format = function(f)
{
    if (!this.valueOf())
        return '&nbsp;';

    var d = this;

    return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/gi,
        function($1)
        {
            switch ($1.toLowerCase())
            {
            case 'yyyy': return d.getFullYear();
            case 'mmmm': return gsMonthNames[d.getMonth()];
            case 'mmm':  return gsMonthNames[d.getMonth()].substr(0, 3);
            case 'mm':   return (d.getMonth() + 1).toString().zp(2);
            case 'dddd': return gsDayNames[d.getDay()];
            case 'ddd':  return gsDayNames[d.getDay()].substr(0, 3);
            case 'dd':   return d.getDate().toString().zp(2);
            case 'hh':   return ((h = d.getHours() % 12) ? h : 12).zp(2);
            case 'nn':   return d.getMinutes().toString().zp(2);
            case 'ss':   return d.getSeconds().toString().zp(2);
            case 'a/p':  return d.getHours() < 12 ? 'a' : 'p';
            }
        }
    );
}	

var curr = new Date(); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = (new Date(curr.setDate(first))).format('dd/mm/yyyy');
var lastday = (new Date(curr.setDate(last))).format('dd/mm/yyyy');

document.write("Information is for week ending "+ lastday);
Regards,
Niral Soni
niralsoni is offline   Reply With Quote
Old 01-29-2013, 04:35 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Niral Sony -

That is fine, but takes no account of what the OP requires:-

I would like it it to read:
Information is for week ending 02/02/2013
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 01-30-2013, 12:33 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,232
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm...Philip, that is *exactly* what it returns.

It's a cute solution, but given how much work it is doing it seems like overkill for underperformance.

For example, what if I *wanted* only 1 digit day numbers? And 2 digit years?
e.g., 3 Feb 13

Or I wanted a 24 hour clock?

Seems to me, if you are going to go to this much trouble, you might as well cover all the bases.

And that "zp" method is cute, but INCREDIBLE over kill for dates.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 01-30-2013, 01:21 AM   PM User | #8
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,460
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
Seems to me, if you are going to go to this much trouble, you might as well cover all the bases.
You mean like being able to display the date as a julian day number or the time in the decimal format that swatch invented or with a suffix indicating whether it is daylight saving time or not - or even display the date as a calendar for the month with the day highlighted.

Like I did with the script at http://javascriptexample.net/dollarD.php
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-30-2013, 02:51 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,232
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Really nice. But I have a ton of suggestions. <grin/>

You really want them?

I'd like to see stuff such as
Code:
    var diff = d1.difference( d2, Date.MINUTE );
I don't see how it's possible to add "static" date members such as Date.MINUTE to a prototype, so if it's not, I'd settler for something simpler. Such as:
Code:
var DATE = {
    SUNDAY : 0,
    MONDAY : 1,
    ...
    SATURDAY : 6,
    JANUARY : 0,
    ...
    DECEMBER : 11,
    SECOND : 1000,
    MINUTE : 60000,
    ...
};
(Not fixed at all on the values for SECOND, MINUTE, etc. Might be cleaner to use characters or negative numbers for their values. Implementation not important.)

And then we would do:
Code:
    var diff = d1.difference( d2, DATE.MINUTE );  // DATE, not Date
Question: Does your toCalendar( ) routine provide the ability to "pick" dates and get them back in particular string formats? Notably DD/MM/YYYY, MM/DD/YYYY, and YYYY/MM/DD? Or does it just produce a Date() object and then one uses your format method to produce what is desired?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 01-30-2013, 06:17 AM   PM User | #10
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,460
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
Really nice. But I have a ton of suggestions. <grin/>

You really want them?
I am always open to suggestions - that's the way that scripts gradually get improved.

Quote:
Originally Posted by Old Pedant View Post
I'd like to see stuff such as
Code:
    var diff = d1.difference( d2, Date.MINUTE );
I don't see how it's possible to add "static" date members such as Date.MINUTE to a prototype, so if it's not, I'd settler for something simpler. Such as:
Code:
var DATE = {
    SUNDAY : 0,
    MONDAY : 1,
    ...
    SATURDAY : 6,
    JANUARY : 0,
    ...
    DECEMBER : 11,
    SECOND : 1000,
    MINUTE : 60000,
    ...
};
(Not fixed at all on the values for SECOND, MINUTE, etc. Might be cleaner to use characters or negative numbers for their values. Implementation not important.)

And then we would do:
Code:
    var diff = d1.difference( d2, DATE.MINUTE );  // DATE, not Date
I'll have to think about that one. The problem with the Date object in JavaScript is that it isn't implemented as a proper object and so you can't use inheritance with it at all - I had to find an alternate way to create an extended object. So implementing static data members would work a bit differently and might be easier to actually implement using the prototype.

Quote:
Originally Posted by Old Pedant View Post
Question: Does your toCalendar( ) routine provide the ability to "pick" dates and get them back in particular string formats? Notably DD/MM/YYYY, MM/DD/YYYY, and YYYY/MM/DD? Or does it just produce a Date() object and then one uses your format method to produce what is desired?
Not yet - it is on my list of things to add in the near future as I have several scripts that will be able to use that calendar code once it has that functionality.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-30-2013, 06:59 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,232
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Great. I really like the idea of being able to use "constants" of the form DATE.JANUARY (or whatever the prefix needs to be) as well as having "generic" functions for things like
Code:
    var d2 = d1.dateAdd( 30, DATE.MINUTE );
    var seconds = d1.dateDiff( d2, DATE.SECOND );
    and so on
You probably recognize the direction I'm going from MySQL:
Code:
    date_add( somedate, INTERVAL 30 MINUTE );
and so on.

Hmmm...I guess we could opt for INTERVAL.MINUTE but what doesn't for for JANUARY and so on.

Keep at it! I'll peek in from time to time. Thanks!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 01-30-2013, 09:06 AM   PM User | #12
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,460
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
Question: Does your toCalendar( ) routine provide the ability to "pick" dates and get them back in particular string formats? Notably DD/MM/YYYY, MM/DD/YYYY, and YYYY/MM/DD? Or does it just produce a Date() object and then one uses your format method to produce what is desired?
New answer as I just changed it.

Now if you pass a function as a parameter to the toCalendar() call then clicking on a date in the calendar will call that function passing the year, month and day that was clicked. In that function you could then use those values to format the date however you like and put it where ever you like.

Still thinking about how to best implement the constants - I'm tempted toward using the prototype as my extended date object doesn't use that for anything yet.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Tags
date formats

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:31 AM.


Advertisement
Log in to turn off these ads.