If you need slightly less control over the formatting than the currently accepted answer,Date#toLocaleDateStringcan be used to create a standard locale-specific rendering. Thelocaleandoptionsparameters let the application specify the language in which the formatting convention should be used, and allow some customization of rendering.
Option key example:
date: Representative of the day. Possible values are "number", "2 digits".
Working days: Representation of working days. Possible values are "narrow", "short", "long".
Year: Annual representation. Possible values are "number", "2 digits".
Month: Representation of month. Possible values are "numeric", "2-digit", "narrow", "short", "long".
Hour: Representation of hours. Possible values are "number", "2 digits".
minute:Representation of minutes. Possible values are "number", "2 digits".
the second: representative of the second. Possible values are 'number', 2 digits.
12 o'clock: Representation of time format. Accepts boolean true or false
All these keys are optional. You can change the number of option values as per your requirement and this will also reflect the presence of each date time term.
Note: If you only want to configure content options but still use the current locale, passingnullfor the first argument will result in an error. Please useundefinedinstead.
For different languages:
"en-US":American English
"en-GB":For British English
"hi-IN":Hindi
"ja-JP":日本语
More language options are available to you.
For example
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; var today = new Date(); console.log(today.toLocaleDateString("en-US")); // 9/17/2016 console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016 console.log(today.toLocaleDateString("hi-IN", options)); // शनिवार, 17 सितंबर 2016
You can also use thetoLocaleString()method to achieve the same purpose. The only difference is that this function provides the time when you don't pass any options.
For custom delimited date formats you must extract the date (or time) Component from aDateTimeFormatobject (i.e. part ofECMAScript Internationalization API) and then manually create the string with your desired delimiter.
To do this, you can useDateTimeFormat# formatToParts. you can Destructuring the array, but this is not ideal since the array output depends on regional settings:
{ // example 1 let formatter = new Intl.DateTimeFormat('en'); let example = formatter.formatToParts(); console.log(example); } { // example 2 let formatter = new Intl.DateTimeFormat('hi'); let example = formatter.formatToParts(); console.log(example); }
If you need slightly less control over the formatting than the currently accepted answer,
Date#toLocaleDateString
can be used to create a standard locale-specific rendering. Thelocale
andoptions
parameters let the application specify the language in which the formatting convention should be used, and allow some customization of rendering.Option key example:
Representative of the day.
Possible values are "number", "2 digits".
Representation of working days.
Possible values are "narrow", "short", "long".
Annual representation.
Possible values are "number", "2 digits".
Representation of month.
Possible values are "numeric", "2-digit", "narrow", "short", "long".
Representation of hours.
Possible values are "number", "2 digits".
Possible values are "number", "2 digits".
representative of the second.
Possible values are 'number', 2 digits.
Representation of time format.
Accepts boolean true or false
All these keys are optional. You can change the number of option values as per your requirement and this will also reflect the presence of each date time term.
Note: If you only want to configure content options but still use the current locale, passing
null
for the first argument will result in an error. Please useundefined
instead.For different languages:
More language options are available to you.
For example
You can also use the
toLocaleString()
method to achieve the same purpose. The only difference is that this function provides the time when you don't pass any options.references:
toLocaleString( )
toLocaleDateString( )
For custom delimited date formats you must extract the date (or time) Component from a
DateTimeFormat
object (i.e. part ofECMAScript Internationalization API) and then manually create the string with your desired delimiter.To do this, you can use
DateTimeFormat# formatToParts
. you can Destructuring the array, but this is not ideal since the array output depends on regional settings: