开发者

js Date toLocaleString

I use the 3 browsers to output this result.

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().toLoca开发者_StackOverflowleString()
> "Sun Sep 4 21:46:03 2011"

why not the same output result? timezoom?


It depends on the configuration of the computer, 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.com/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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜