If you need slightly less control over formatting than the currently accepted answer, Date#toLocaleDateString
can be used to create standard locale-specific renderings. The locale
and options
arguments let applications specify the language whose formatting conventions should be used, and allow some customization of the rendering.
Options key examples:
- day:
The representation of the day.
Possible values are "numeric", "2-digit".
- weekday:
The representation of the weekday.
Possible values are "narrow", "short", "long".
- year:
The representation of the year.
Possible values are "numeric", "2-digit".
- month:
The representation of the month.
Possible values are "numeric", "2-digit", "narrow", "short", "long".
- hour:
The representation of the hour.
Possible values are "numeric", "2-digit".
- minute:
The representation of the minute.
Possible values are "numeric", "2-digit".
- second:
The representation of the second.
Possible values are "numeric", 2-digit".
- hour12:
The representation of time format.
Accepts boolean true or false
All these keys are optional. You can change the number of options values based on your requirements, and this will also reflect the presence of each date time term.
Note: If you would only like to configure the content options, but still use the current locale, passing null
for the first parameter will cause an error. Use undefined
instead.
For different languages:
- "en-US": For American English
- "en-GB": For British English
- "hi-IN": For Hindi
- "ja-JP": For Japanese
You can use more language options.
For example
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today = new Date();
console.log(today.toLocaleDateString("en-US")); // 9/17/2016
console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
console.log(today.toLocaleDateString("hi-IN", options)); // शनिवार, 17 सितंबर 2016
You can also use the toLocaleString()
method for the same purpose. The only difference is this function provides the time when you don't pass any options.
// Example
9/17/2016, 1:21:34 PM
References:
Simple solution: Replace the space in your date
string with a "T".
(However, to be completely technically correct, you should also include a time zone indicator at the end, either an additional "Z" to indicate UTC, i.e. Coordinated Universal Time, or "+hh:mm" or "-hh:mm" to indicate a time zone offset.)
The MDN site for Date.parse()
writes:
Because of the variances in parsing of date strings, ...results are inconsistent, especially across different ECMAScript implementations where strings like "2015-10-12 12:00:00" may be parsed to as NaN, UTC or local timezone.
and
The date time string may be in ISO 8601 format.
The ISP 8601 specs referred to above writes:
The formats are as follows. Exactly the components shown here must be present, with exactly this punctuation. Note that the "T" appears literally in the string, to indicate the beginning of the time element, as specified in ISO 8601.
and
Complete date plus hours, minutes, seconds and a decimal fraction of a
second YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
Here is your code re-written replacing the space in date
with a "T". If that doesn't work in your browser, add a "Z" or time zone offset at the end of date
.
var date ='2016-03-02T09:12:14.989522';
var d = new Date(date),
month = d.getMonth() + 1,
day = d.getDate(),
year = d.getFullYear(),
hour = d.getHours(),
min = d.getMinutes();
document.write([day, month, year].join('-') + ' ' + [hour, min].join(':'));
Best Answer
You prototype a method so you never have to do this irritating task again:
(and yes you could chain those replaces, but it's not here for readability before anyone asks)
As requested, additional prototypes to support the above snippet.
and usage example: