JavaScript:Date 的 toString() 和 toLocaleString() 方法的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11945272/
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
JavaScript: Difference between toString() and toLocaleString() methods of Date
提问by M. Ahmad Zafar
I am unable to understand the difference between the toString()
and toLocaleString()
methods of a Date
object in JavaScript. One thing I know is that toString()
will automatically be called whenever the Date
objects needs to be converted to string.
我无法理解JavaScript 中对象的toString()
和toLocaleString()
方法之间的区别Date
。我知道的一件事是,toString()
只要Date
对象需要转换为字符串,就会自动调用。
The following code returns identical results always:
以下代码始终返回相同的结果:
?var d = new Date();
document.write( d + "<br />" );
document.write( d.toString() + "<br />" );
document.write( d.toLocaleString() );
? And the output is:
? 输出是:
Tue Aug 14 2012 08:08:54 GMT+0500 (PKT)
Tue Aug 14 2012 08:08:54 GMT+0500 (PKT)
Tue Aug 14 2012 08:08:54 GMT+0500 (PKT)
回答by phenomnomnominal
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleString
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleString
Basically, it formats the Date to how it would be formatted on the computer where the function is called, e.g. Month before Day in US, Day before Month in most of the rest of the world.
基本上,它将日期格式化为在调用该函数的计算机上的格式,例如美国的前一天,世界其他大部分地区的前一个月。
EDIT:
编辑:
Because some others pointed out that the above reference isn't necessary reliable, how's this from the ECMAScript spec:
因为其他一些人指出上述参考不一定可靠,所以ECMAScript 规范如何:
15.9.5.2 Date.prototype.toString ( )
This function returns a String value. The contents of the String are implementation->> dependent, but are intended to represent the Date in the current time zone in a convenient, human-readable form.
15.9.5.5 Date.prototype.toLocaleString ( )
This function returns a String value. The contents of the String are implementation->>dependent, but are intended to represent the Date in the current time zone in a convenient, human-readable form that corresponds to the conventions of the host environment‘s current locale.
15.9.5.2 Date.prototype.toString ( )
此函数返回一个字符串值。String 的内容依赖于 implementation->>,但旨在以方便的、人类可读的形式表示当前时区中的 Date。
15.9.5.5 Date.prototype.toLocaleString ( )
此函数返回一个字符串值。String 的内容是 implementation->> 相关的,但旨在以一种方便的、人类可读的形式表示当前时区中的 Date,该形式对应于主机环境的当前语言环境的约定。
Since you can hopefully assume that most implementations will reflect the specification, the difference is that toString()
is just required to be readable, toLocaleString()
should be readable in a format that the should match the users expectations based on their locale.
由于您可以假设大多数实现将反映规范,不同之处在于toString()
它只需要可读,toLocaleString()
应该以符合用户期望的格式读取基于他们的语言环境。
回答by The Internet
Converts a date to a string, using the operating system's locale's conventions.
使用操作系统的区域设置约定将日期转换为字符串。
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleString
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleString
toLocaleString behaves similarly to toString when converting a year that the operating system does not properly format.
在转换操作系统格式不正确的年份时,toLocaleString 的行为与 toString 类似。
回答by Viktor Soroka
I am just checked in console of the Chrome for date and found the difference in the presentation format. Hope this could help.
我刚刚在 Chrome 的控制台中检查了日期,发现了演示格式的差异。希望这会有所帮助。
var d = new Date();
console.log(d.toLocaleString()); //"04.09.2016, 15:42:44"
console.log(d.toString()); //"Sun Sep 04 2016 15:42:44 GMT+0300 (FLE Daylight Time)"
回答by RobG
Lots of references, but none are authoritative. Note that Mozilla's documentation is for JavaScript, which is their version of ECMAScript for browsers. Other browsers use other implementations and therefore, while the MDN documentation is useful, it is not authoritative (it is also a community wiki, so not even official Mozilla documentation) and does not necessarily apply to other browsers.
很多参考文献,但没有一个是权威的。请注意,Mozilla 的文档是针对 JavaScript 的,这是他们用于浏览器的 ECMAScript 版本。其他浏览器使用其他实现,因此,虽然 MDN 文档很有用,但它并不权威(它也是社区 wiki,所以甚至不是 Mozilla 官方文档)并且不一定适用于其他浏览器。
The definitive reference is the ECMAScript Language specification, where the behaviour of both Date.prototype.toStringand Date.prototype.toLocaleStringare explained in browser independent terms.
明确的参考是 ECMAScript 语言规范,其中Date.prototype.toString和Date.prototype.toLocaleString的行为都以浏览器无关的术语进行解释。
Notable is the for both methods, the string is implementation dependent, which means that different browsers will return different strings.
值得注意的是,对于这两种方法,字符串是依赖于实现的,这意味着不同的浏览器将返回不同的字符串。