What is the output in each browser?
– Matt Ball
CommentedSep 4, 2011 at 14:06
chrome: new Date().toLocaleString() "Sun Sep 04 2011 21:40:04 GMT+0800 (HKT)" safari: >>> new Date().toLocaleString() "2011年9月4日 下午09时54分51秒格林尼治标准时间+0800" FF: >>> new Date().toLocaleString() "Sun Sep 4 21:46:03 2011"
– Thinking80s
CommentedSep 4, 2011 at 14:10
I hesitated calling this a duplicate of stackoverflow./questions/2115725/… but this is the real question, the other one is only a good question by luck!
– Stefano
CommentedNov 16, 2011 at 15:46
Try to use localization parameter. Example: new Date().toLocaleString("en-us")
– Bohdan Kuts
CommentedJul 22, 2015 at 13:05
Add a ment
|
3 Answers
3
Reset to default
2
It depends on the configuration of the puter, the user's preferred date format, obviously the user's locale, and how the browser determines this.
You should really prefer using a proper date library such as datejs for formatting.
See their Date.toString() and format specifiers.
That's a bug in webkit, actually; in particular in Chrome but Safari is indeed affected too: http://code.google./p/chromium/issues/detail?id=3607
toLocaleString() does not translate to the locale!
The worst is, it's closed as WontFix. How is that possible? We should try and re-open it. The conclusion on the bug is that somewhen a new javascript globalization apis (that is well explained here) will appear. That doesn't sound like a solution to me!
In any case, if possible, follow @arnaud576875 suggestion to use datejs which is old but still very good.
Check this link
And this example:
var event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// British English uses day-month-year order and 24-hour time without AM/PM
console.log(event.toLocaleString('en-GB', { timeZone: 'UTC' }));
// expected output: 20/12/2012, 03:00:00
// Korean uses year-month-day order and 12-hour time with AM/PM
console.log(event.toLocaleString('ko-KR', { timeZone: 'UTC' }));
// expected output: 2012. 12. 20. 오전 3:00:00