Get String in YYYYMMDD format from JS date object?

Ask a question
4

I’m trying to use JS to turn a date object into a string in YYYYMMDD format. I want to find an easier way than concatenating Date.getYear(), Date.getMonth() and Date.getDay()? Is there?

Coin iconoffer bounty
add comment

2 Answers

5

There is a popular library momentjs with works with date and time.

add comment
0

You can use Intl object.
example:

new Intl.DateTimeFormat('en-US').format(YOUR_DATE_OBJECT);

it can return in any requested locale and any format.

see more about Intl.DateTimeFormat in MDN

add comment

Your Answer