Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. One thing I know is that toString() will automatically be called whenever the Date objects needs to be converted to string.

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)
like image 325
M. Ahmad Zafar Avatar asked Aug 14 '12 03:08

M. Ahmad Zafar


People also ask

What does the toLocaleString () method do in JS?

The toLocaleString() method returns a string with a language-sensitive representation of this date. In implementations with Intl. DateTimeFormat API support, this method simply calls Intl. DateTimeFormat .

How do you get a date from toLocaleString?

To get the current date and time in JavaScript, you can use the toLocaleString() method, which returns a string representing the given date according to language-specific conventions. To display only the time, you can use the toLocaleTimeString() method.


3 Answers

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

toLocaleString behaves similarly to toString when converting a year that the operating system does not properly format.

like image 44
The Internet Avatar answered Sep 25 '22 03:09

The Internet


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:

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.

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.

like image 183
phenomnomnominal Avatar answered Sep 25 '22 03:09

phenomnomnominal


I am just checked in console of the Chrome for date and found the difference in the presentation format. 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)"
like image 30
Viktor Soroka Avatar answered Sep 22 '22 03:09

Viktor Soroka