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?
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.
The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
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.
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.
Steven Levithan's dateFormat()
(only 1.2KB when minified and gziped!) should do just what you need.
// Formatting in: May 18, 2011 7:45 AM
var formattedDate = new Date().format('mmm dd, yyyy h:mm TT');
document.getElementById("time").innerHTML= formattedDate;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With