How to format date in JavaScript?
P粉914731066
P粉914731066 2023-08-23 10:10:06
0
2
564
<p>How to format a Javascript <code>Date</code> object into a string? (Preferably in the following format: <code>10-Aug-2010</code>)</p>
P粉914731066
P粉914731066

reply all(2)
P粉253800312

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. The locale and options parameters let the application specify the language in which the formatting convention should be used, and allow some customization of rendering.

Option key example:

  1. date:
    Representative of the day.
    Possible values ​​are "number", "2 digits".
  2. Working days:
    Representation of working days.
    Possible values ​​are "narrow", "short", "long".
  3. Year:
    Annual representation.
    Possible values ​​are "number", "2 digits".
  4. Month:
    Representation of month.
    Possible values ​​are "numeric", "2-digit", "narrow", "short", "long".
  5. Hour:
    Representation of hours.
    Possible values ​​are "number", "2 digits".
  6. minute: Representation of minutes.
    Possible values ​​are "number", "2 digits".
  7. the second:
    representative of the second.
    Possible values ​​are 'number', 2 digits.
  8. 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, passing null for the first argument will result in an error. Please use undefined instead.

For different languages:

  1. "en-US":American English
  2. "en-GB": For British English
  3. "hi-IN": Hindi
  4. "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 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.

// Example
9/17/2016, 1:21:34 PM

references:

P粉457445858

For custom delimited date formats you must extract the date (or time) Component from a DateTimeFormat object (i.e. part of ECMAScript 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:

{ // 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);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template