9

Possible Duplicate:
How do I display a date/time in the user's locale format and time offset?

Hi - simple question - I just want to take this:

document.getElementById("time").innerHTML= new Date();

and format it into something legible, like this:

May 18, 2011 7:45 AM

making sure it is localized to whomever might be seeing it. Currently, it prints out as this:

Wed May 18 2011 07:46:25 GMT-0400 (EDT)

How do I do this?

Community
  • 1
  • 1
mheavers
  • 29,530
  • 58
  • 194
  • 315
  • Not really sure why this question is deserving of a negative vote... – mheavers May 18 '11 at 12:28
  • Not sure how this can be a bad thing if the responses bring up new information, such as Gary Green's plugin suggestion below. I did a search for this and the solutions either weren't clear to me, the examples were overly complex, or they didn't fit my exact scenario. Everyone has a different level of understand of things. – mheavers May 19 '11 at 10:29

2 Answers2

12

Steven Levithan's dateFormat() (only 1.2KB when minified and gziped!) should do just what you need.

Javascript

// Formatting in: May 18, 2011 7:45 AM
var formattedDate = new Date().format('mmm dd, yyyy h:mm TT');

document.getElementById("time").innerHTML= formattedDate;
Community
  • 1
  • 1
Gary Green
  • 22,045
  • 6
  • 49
  • 75
9

Look up the reference for

  Date.toLocaleString()

  Date.toLocaleDateString(), and 

  Date.toLocaleTimeString().
sandip
  • 3,279
  • 5
  • 31
  • 54
Jon Trauntvein
  • 4,453
  • 6
  • 39
  • 69
  • I tried this, but it still prints: Wed May 18 2011 08:09:12 GMT-0400 (EDT). I don't want all that GMT crap. – mheavers May 18 '11 at 12:09
  • 1
    Just used the following: var d = new Date(); var dd = d.toDateString(); var dt = d.toTimeString(); document.getElementById("time").innerHTML = dd + " " + dt; – mheavers May 18 '11 at 12:31