最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - js Date toLocaleString - Stack Overflow

programmeradmin0浏览0评论

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().toLocaleString()
> "Sun Sep 4 21:46:03 2011"

why not the same output result? timezoom?

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().toLocaleString()
> "Sun Sep 4 21:46:03 2011"

why not the same output result? timezoom?

Share Improve this question edited Sep 4, 2011 at 14:20 Uwe Keim 40.8k61 gold badges189 silver badges304 bronze badges asked Sep 4, 2011 at 14:04 Thinking80sThinking80s 2,8703 gold badges17 silver badges16 bronze badges 4
  • What is the output in each browser? – Matt Ball Commented Sep 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 Commented Sep 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 Commented Nov 16, 2011 at 15:46
  • Try to use localization parameter. Example: new Date().toLocaleString("en-us") – Bohdan Kuts Commented Jul 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

发布评论

评论列表(0)

  1. 暂无评论