Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,086
    Thanks
    38
    Thanked 498 Times in 492 Posts
    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 !!!

  • Users who have thanked jmrker for this post:

    mangisqa (01-29-2013)

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Excellent- worked like a charm!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.
    Last edited by Philip M; 01-29-2013 at 04:39 PM.

    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.

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,516
    Thanks
    77
    Thanked 4,379 Times in 4,344 Posts
    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.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,516
    Thanks
    77
    Thanked 4,379 Times in 4,344 Posts
    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.

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,516
    Thanks
    77
    Thanked 4,379 Times in 4,344 Posts
    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.

  • #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •