javascript js 日期 toLocaleString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7299695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:39:31  来源:igfitidea点击:

js Date toLocaleString

javascriptdate

提问by Thinking80s

I use the 3 browsers to output this result.

我使用 3 个浏览器来输出这个结果。

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?

为什么不一样的输出结果?时间缩放?

回答by Arnaud Le Blanc

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 datejsfor formatting.

您真的应该更喜欢使用适当的日期库(例如datejs)进行格式化。

See their Date.toString()and format specifiers.

查看他们的Date.toString()format specifiers

回答by Stefano

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

实际上,这是 webkit 中的一个错误;特别是在 Chrome 中,但 Safari 确实也受到了影响:http: //code.google.com/p/chromium/issues/detail?id=3607

toLocaleString() does not translate to the locale!

toLocaleString() 不会转换为语言环境!

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!

最糟糕的是,它已作为 WontFix 关闭。这怎么可能?我们应该尝试重新打开它。关于这个错误的结论是,某个新的javascript 全球化 apis这里有很好的解释)将出现。这对我来说听起来不是一个解决方案!

In any case, if possible, follow @arnaud576875 suggestion to use datejswhich is old but still very good.

在任何情况下,如果可能,请遵循@arnaud576875 建议使用旧但仍然很好的datejs

回答by Daniel Carpio Contreras

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