Date.prototype.toLocaleString() in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The javascript date tolocalestring() method is used for returning a string with a language-sensitive representation of the current date. The javascript date tolocalestring() method can be available from the objects such as Date and Number from JavaScript. This method aims to create a local sensitive (it is a language-specific format to use) value that corresponds to your date and number value.

Syntax of Date.prototype.toLocaleString() in JavaScript

The date tolocalestring() method belongs Date class of JavaScript. The syntax of the javascript date tolocalestring() method is as follows:

Parameters of Date.prototype.toLocaleString() in JavaScript

The parameters of the javascript date tolocalestring() method are as follows: There are two parameters of the javascript date tolocalestring() methods. The first one is locales and the second one is options.

  • locales: This is an optional parameter. This parameter is an array of locale strings that contains one or more than one language tag. The value of locales decides the specific format of the language in our application.

Following are the different values of locales and their conventions that we need to know.

localescountry
ar-SAArabic (Saudi Arabia)
bn-BDBangla (Bangladesh)
bn-INBangla (India)
cs-CZCzech (Czech Republic)
da-DKDanish (Denmark)
de-ATAustrian German
de-CH"Swiss" German
de-DEStandard German (as spoken in Germany)
el-GRModern Greek
Modern GreekAustralian English
en-CACanadian English
en-GBBritish English
en-IEIrish English
en-INIndian English
en-NZNew Zealand English
en-USUS English
en-ZAEnglish (South Africa)
es-ARArgentine Spanish
es-CLChilean Spanish
es-COColombian Spanish
es-ESCastilian Spanish
es-MXMexican Spanish
es-USAmerican Spanish
fi-FIFinnish (Finland)
fr-BEBelgian French
fr-CACanadian French
fr-CH"Swiss" French
fr-FRStandard French
he-ILHebrew (Israel)
hi-INHindi (India)
hu-HUHungarian (Hungary)
id-IDIndonesian (Indonesia)
it-CH"Swiss" Italian
it-ITStandard Italian
ja-JPJapanese (Japan)
ko-KRKorean (Republic of Korea)
nl-BEBelgian Dutch
nl-NLStandard Dutch
no-NONorwegian (Norway)
pl-PLPolish (Poland)
pt-BRBrazilian Portuguese
pt-PTEuropean Portuguese
ro-RORomanian (Romania)
ru-RURussian (Russian Federation)
sk-SKSlovak (Slovakia)
sv-SESwedish (Sweden)
ta-INIndian Tamil
ta-LKSri Lankan Tamil
th-THThai (Thailand)
tr-TRTurkish (Turkey)
zh-CNMainland China, simplified characters
zh-HKHong Kong, traditional characters
zh-TWTaiwan, traditional characters
  • options: This is also an optional parameter. This parameter is an object where we can set the properties and it is used for customizing the return value. The various legal properties and legal values of options are:

    • dateStyle: "full","long", "medium", "short"
    • timeStyle: "full", "long", "medium", "short"
    • localeMatcher: "best-fit" (default), "lookup"
    • hourCycle: "h11", "h12", "h23", "h24"
    • formatMatcher: "basic", "best-fit" (default)
    • weekday : "long", "short", "narrow"
    • year: "2-digit", "numeric"
    • month: "2-digit", "long", "narrow", "numeric", "short"
  • Note: If both the above parameters (locales and options) are present in the method, then this method returns the string value corresponding to the local time zone of that particular user.

Return Value of Date.prototype.toLocaleString() in JavaScript

The return value of the javascript date tolocalestring() method is as follows:

  • Return Value: The javascript date tolocalestring() method returns a string as a return value. This string value represents the date and time in the specific format that is specified by the locale.

Example

In the above sections, we have discussed the various aspects of the javascript date tolocalestring() method. Now let us see a sort and simple example of this method to understand how the javascript date tolocalestring() method works.

Output:

What is Date.prototype.toLocaleString() in JavaScript?

The javascript date tolocalestring() method is defined as the method that is used for returning a string value that corresponds to the local time of the argument that we pass in the method as locales. This string value is associated with a language-sensitive representation of the current date. The javascript date tolocalestring() method can be available from the objects such as Date and Number from JavaScript.

The javascript date tolocalestring() method is able to call all the types of data type values that we can define in our code. For example, if we want a date that we need to print in the format of month-day-year then we will use the method like:

The output of this code will be like this:

We have to keep in mind that Mon Mar 14 2022 10:10:35 GMT+0800 (Singapore Standard Time) 3/14/2022, 10:10:35 AM returns a string value only, so we can not call the date object method on the returns value. If we do this, then we will get a TypeError.

The javascript toLocaleString()is a feature of the ECMAScript (also known as ES1). In this method, there are two parameters that are used to perform with this method more user-friendly and efficient as the user can describe their need and demands in these optional parameters. The locales parameter is used to describe which local language and date format you want to use and the options parameter is used for customizing the return value. If both these parameters are optional. so if these parameters are not present in the method then this method returns the string value of the given reference time according to the local time zone of the user's system. And the options parameter is used for customizing the return value.

More Examples

Now let us see some more examples of the javascript date tolocalestring() method in various cases. Each of the below examples is discussed with output and explanation where ever necessary.

  • Example 1. In this example, we will use the javascript date tolocalestring() method for returning a string that will correspond to a language-sensitive representation of the date given in the code.

Output:

Explanation: In the above example, we simply declared a variable named "date". Then we assigned the variable date and time for reference that will later be used to convert them into a string with respect to local time. The values of date and time are separated by commas in the format of (YYYY, MM, DD, HOUR, MINUTE, SECOND). Then we used the console.log to print the output and assigned this the method date.LocaleString.

  • Example 2. In this example, we will use the javascript date tolocalestring() method for returning a string checking for locales that will correspond to a language-sensitive representation of the date given in the code.

Output:

Explanation: In the above examples, we used the javascript date tolocalestring() method to show the variations in local time date and time format. For getting the format of the language, that we are using in the interface of the application we make sure that the language is using the options argument. First, we declared the variable named "date" and then assigned it the value that will be used as a reference date. This reference is later converted into the local date and time formats by using the console.log command and assigning the locales argument ('en-US', 'ko-KR', and 'ar-EG' for US, Korea, and Arab respectively) to it.

  • Example 3. In this example, we will use the javascript date tolocalestring() method for returning a string checking for options that will correspond to a language-sensitive representation of the date given in the code.

Output:

Explanation: In the above examples, we used the javascript date tolocalestring() method to show the variations in local time date and time format. For getting the format of the language, that we are using in the interface of the application we make sure that the language is using the options argument.

Supported Browsers

The javascript date tolocalestring() method is supported by almost all modern browsers. Given below are the name of some popular browsers that supports the javascript date tolocalestring() method.

Browser NameSupported on versions
Mozilla Firefox1.0 and above
Microsoft Edge12.0 and above
Google Chrome1 and above
Safari1.0 and above
Opera3.0 and above

Conclusion

  • The javascript date tolocalestring() method is defined as the method that is used for returning a string value. This string value is associated with a language-sensitive representation of the current date.
  • There are two parameters of the javascript date tolocalestring() methods. The first one is locales and the second one is options.
  • The locale is an optional parameter. This parameter specifies the specific language format that will use and the options is also an optional parameter. This parameter is an object where we can set the properties.
  • The javascript date tolocalestring() method returns a string as a return value that represents the date that corresponds to the language-specific conventions.
  • Various names of popular browsers that support the javascript date tolocalestring() method are Mozilla Firefox. Microsoft Edge, Google Chrome, Safari, and Opera browser.