简体   繁体   English

JavaScript:日期的 toString() 和 toLocaleString() 方法之间的区别

[英]JavaScript: Difference between toString() and toLocaleString() methods of Date

I am unable to understand the difference between the toString() and toLocaleString() methods of a Date object in JavaScript.我无法理解 JavaScript 中Date对象的toString()toLocaleString()方法之间的区别。 One thing I know is that toString() will automatically be called whenever the Date objects needs to be converted to string.我知道的一件事是,每当需要将Date对象转换为字符串时,都会自动调用toString()

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)

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, eg 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. String 的内容依赖于 implementation->>,但旨在以方便的、人类可读的形式表示当前时区中的 Date。

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. 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()应该以一种格式可读,该格式应符合用户基于语言环境的期望。

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 类似。

I am just checked in console of the Chrome for date and found the difference in the presentation format.我刚刚在 Chrome 的控制台中检查了日期,发现了演示格式的差异。 Hope this could help.希望这会有所帮助。

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)"

Lots of references, but none are authoritative.很多参考文献,但没有一个是权威的。 Note that Mozilla's documentation is for JavaScript, which is their version of ECMAScript for browsers.请注意,Mozilla 的文档是针对 JavaScript 的,这是他们用于浏览器的 ECMAScript 版本。 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.其他浏览器使用其他实现,因此,虽然 MDN 文档很有用,但它并不权威(它也是一个社区 wiki,所以甚至不是 Mozilla 官方文档)并且不一定适用于其他浏览器。

The definitive reference is the ECMAScript Language specification, where the behaviour of both Date.prototype.toString and Date.prototype.toLocaleString are explained in browser independent terms.最终参考是 ECMAScript 语言规范,其中Date.prototype.toStringDate.prototype.toLocaleString的行为都以浏览器无关的术语进行解释。

Notable is the for both methods, the string is implementation dependent , which means that different browsers will return different strings.值得注意的是对于这两种方法,字符串是依赖于实现的,这意味着不同的浏览器将返回不同的字符串。

Just to add.只是为了补充。 Apart from Date, it also converts/formats the normal variable.除了日期,它还转换/格式化普通变量。 Both functions used to format/convert the passed parameter to string but how parameter is formatted is the point to look on.这两个函数都用于将传递的参数格式化/转换为字符串,但参数的格式化方式是重点。

toLocalestring() used to return the formatted string based on which geography the function is called. toLocalestring()用于根据调用函数的地理位置返回格式化字符串

For the sake of simplicity.为了简单起见。 Take this example.以这个例子为例。 It shows how toString() won't format the variable but toLocaleSting() will format it based on locale setting of the geography.它显示了如何toString() 不会格式化变量,但toLocaleSting() 将根据地理区域设置对其进行格式化

let number = 1100;
console.log(number.toString()); // "1100"
console.log(number.toLocaleString())  // 1,100

 let number = 1100; console.log(number.toString()); console.log(number.toLocaleString());

It is a great help for programmer in order to avoid to write extra function to format the string or Date.这对于程序员避免编写额外的函数来格式化字符串或日期有很大帮助。 toLocaleString() will take care of this. toLocaleString() 会处理这个问题。

Hope you would find it somewhat helpful & interesting.希望你会觉得它有点帮助和有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM