How to format date in JavaScript?
P粉894008490
P粉894008490 2023-10-08 12:38:51
0
2
396

How to format a Javascript Date object into a string? (Best format: 10-Aug-2010)

P粉894008490
P粉894008490

reply all(2)
P粉611456309

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粉154228483

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!