dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: How To Format Javascript Date

  1. #1
    Join Date
    Jun 2006
    Posts
    112

    How To Format Javascript Date

    Newbie - Attempting to format date and having no success. Any help is appreciated. Here is what I have done so far.
    <Script>
    function chgit() {
    var d = new Date();
    var mm = d.getMonth();
    var dd = d.getDay();
    var yy = d.getFullYear();
    var thedate = (mm dd yy) ;
    document.getElementById("Date_Resolved").value = thedate;
    }
    </Script>
    Last edited by Chamark; 07-18-2013 at 10:17 AM. Reason: [RESOLVED]

  2. #2
    Join Date
    Jun 2006
    Posts
    112
    Figured it out - it is
    <Script>
    function chgit() {
    var d = new Date();
    var mm = d.getMonth()+1;
    var dd = d.getDate();
    var yy = d.getFullYear();
    var thedate = (mm + "/" + dd + "/" + yy) ;
    document.getElementById("Date_Resolved").value = thedate;
    }
    </Script>

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,746
    It depends on what you are wanting to do.

    You could have just as easily use the toGMTString() or the UTC equivalent to obtain a formatted string and using .slice() to cut out what you needed.

    if you want to use the above then

    var d=new Date();

    and applying the value of d to your output field results in a value like "Fri Jul 19 2013 09:31:00 GMT+0100 (GMT Daylight Time)"

    using var d=new Date().toGMTString(); results in a string like "Fri, 19 Jul 2013 08:32:19 GMT"

    using var d=new Date().toUTCString(); results in a string like "Fri, 19 Jul 2013 08:32:19 GMT"

    Your script can be easily compressed by using the "with" clause down to...

    Code:
    function chgit() {
    with( new Date() );
    document.getElementById("Date_Resolved").value = [getMonth()+1,getDate(),getFullYear()].join("/");
    }
    STOP using $ prefix on JavaScript variable names...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Nov 2014
    Posts
    11
    does the var day = d.getDay(); contain a number between 1 and 31 ? if it was included ? I not getting anything ?

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,746
    getDay returns a number 0 to 6 with 0 being Sunday, 1=Monday...

    getDate returns a number 1 to 31
    STOP using $ prefix on JavaScript variable names...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles