Date.prototype.toLocaleString() in JavaScript

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.
locales | country |
---|---|
ar-SA | Arabic (Saudi Arabia) |
bn-BD | Bangla (Bangladesh) |
bn-IN | Bangla (India) |
cs-CZ | Czech (Czech Republic) |
da-DK | Danish (Denmark) |
de-AT | Austrian German |
de-CH | "Swiss" German |
de-DE | Standard German (as spoken in Germany) |
el-GR | Modern Greek |
Modern Greek | Australian English |
en-CA | Canadian English |
en-GB | British English |
en-IE | Irish English |
en-IN | Indian English |
en-NZ | New Zealand English |
en-US | US English |
en-ZA | English (South Africa) |
es-AR | Argentine Spanish |
es-CL | Chilean Spanish |
es-CO | Colombian Spanish |
es-ES | Castilian Spanish |
es-MX | Mexican Spanish |
es-US | American Spanish |
fi-FI | Finnish (Finland) |
fr-BE | Belgian French |
fr-CA | Canadian French |
fr-CH | "Swiss" French |
fr-FR | Standard French |
he-IL | Hebrew (Israel) |
hi-IN | Hindi (India) |
hu-HU | Hungarian (Hungary) |
id-ID | Indonesian (Indonesia) |
it-CH | "Swiss" Italian |
it-IT | Standard Italian |
ja-JP | Japanese (Japan) |
ko-KR | Korean (Republic of Korea) |
nl-BE | Belgian Dutch |
nl-NL | Standard Dutch |
no-NO | Norwegian (Norway) |
pl-PL | Polish (Poland) |
pt-BR | Brazilian Portuguese |
pt-PT | European Portuguese |
ro-RO | Romanian (Romania) |
ru-RU | Russian (Russian Federation) |
sk-SK | Slovak (Slovakia) |
sv-SE | Swedish (Sweden) |
ta-IN | Indian Tamil |
ta-LK | Sri Lankan Tamil |
th-TH | Thai (Thailand) |
tr-TR | Turkish (Turkey) |
zh-CN | Mainland China, simplified characters |
zh-HK | Hong Kong, traditional characters |
zh-TW | Taiwan, 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 Name | Supported on versions |
---|---|
Mozilla Firefox | 1.0 and above |
Microsoft Edge | 12.0 and above |
Google Chrome | 1 and above |
Safari | 1.0 and above |
Opera | 3.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.