17

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)
M. Ahmad Zafar
  • 4,881
  • 4
  • 32
  • 44

5 Answers5

19

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.

Community
  • 1
  • 1
phenomnomnominal
  • 5,427
  • 1
  • 30
  • 48
  • 1
    Yeah but whats the difference? – M. Ahmad Zafar Aug 14 '12 at 03:18
  • 2
    from https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toString - toString always returns a string representation of the date in American English. – phenomnomnominal Aug 14 '12 at 03:21
  • I change the localization settings on my system and then tested. The difference was clear now. Thanks! – M. Ahmad Zafar Aug 14 '12 at 03:32
  • 1
    @phenomnomnominal—that might be what Mozilla does, but it's not necessarily what other browsers do. – RobG Aug 14 '12 at 03:58
  • Not all browsers respect the locale settings when calling `date.toLocaleString()`. I tested this: Chrome and Safari always use US locale settings, while IE, Opera and Firefox correctly use my computers locale settings (in my case Norwegian). – awe Oct 22 '13 at 12:13
  • @phenomnomnominal, I "googled" and guess what? This page showed up in the results while your Mozilla-specific page did not. Please don't condescend others for using Stack Overflow for its intended purpose. – bugged87 May 03 '16 at 20:10
  • Maybe its a [brain tumor](https://www.google.com/search?q=%22I+googled+and+guess+what%3F%22). – showdev Aug 17 '16 at 17:56
5

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.

The Internet
  • 7,959
  • 10
  • 54
  • 89
2

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)"
Viktor Soroka
  • 187
  • 2
  • 4
1

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.

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.

Notable is the for both methods, the string is implementation dependent, which means that different browsers will return different strings.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Hmm so my question was valid :-) There is confusion on the exact difference and browser behavior – M. Ahmad Zafar Aug 14 '12 at 04:24
  • @MuhammadAhmadZafar—Yes, a valid question. The bottom line is that while most browsers seem to return a US–centric date format for *toString*, that is entirely up to the developers and may well change since the majority of browser users do not use the US date format normally. Also, even when taking regional settings into account, some may return say 10 August, 2012 and others 10/08/2012. – RobG Aug 15 '12 at 03:30
0

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.

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.

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.

Hope you would find it somewhat helpful & interesting.

Alok Ranjan
  • 967
  • 10
  • 10