Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format javascript current date and time [duplicate]

Tags:

javascript

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?

like image 768
mheavers Avatar asked May 18 '11 11:05

mheavers


People also ask

What date format is DD MMM YYYY JavaScript?

There is no native format in JavaScript for” dd-mmm-yyyy”. To get the date format “dd-mmm-yyyy”, we are going to use regular expression in JavaScript. The regular expression in JavaScript is used to find the pattern in a string.

What is now () in JavaScript?

The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

Does new date () Return current time?

The expression new Date() returns the current time in internal format, as an object containing the number of milliseconds elapsed since the start of 1970 in UTC.

How do you get current date and time in JavaScript?

In JavaScript, we can easily get the current date or time by using the new Date() object. By default, it uses our browser's time zone and displays the date as a full text string, such as "Fri Jun 17 2022 10:54:59 GMT+0100 (British Summer Time)" that contains the current date, time, and time zone.


1 Answers

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;
like image 89
Gary Green Avatar answered Sep 21 '22 15:09

Gary Green