javascript tutorial - [Solved-5 Solutions] Format a JavaScript date - javascript - java script - javascript array
Problem:
How to format a JavaScript date?
Solution 1:
Solution 2:
Use the date.format library :
returns:
Solution 3:
sing this function instead:
Solution 4:
- quickly format the date using plain JavaScript, use
getDate
code>,getMonth + 1
,getFullYear
,getHours
andgetMinutes
:
Solution 5:
Use toLocaleDateString();
The toLocaleDateString() method returns a string with a language-sensitive representation of the date portion of the date. The locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function.
The values you can passed in options for different keys:
day:
- The representation of the day.
- Possible values are "numeric", "2-digit".
The representation of the day. Possible values are "numeric", "2-digit".
weekday:
- The representation of the weekday.
- Possible values are "narrow", "short", "long".
year:
- The representation of the year.
- Possible values are "numeric", "2-digit".
month:
- The representation of the month.
- Possible values are "numeric", "2-digit", "narrow", "short", "long".
hour:
- The representation of the hour.
- Possible values are "numeric", "2-digit".
minute:
- The representation of the minute.
- Possible values are "numeric", "2-digit".
second:
- The representation of the second.
- Possible values are "numeric", 2-digit".
All these keys are optional.You can change the number of options values based on your requirements.
For different languages:
- "en-US": For English
- "hi-IN": For Hindi
- "ja-JP": For Japanese
You can use more language options.
For example