www.webdeveloper.com
Results 1 to 3 of 3

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 09: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,216
    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("/");
    }
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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