shamon shamsudeen
shamon shamsudeen

Reputation: 5848

moment js:month format number not starting with zero

I am trying to format a UTC time stamp to date string and I want month index to start with 0

eg:

0 - January,

1 - February

and so on.

moment(1516102320000).format('YYYY-M-DD-h-mm-s');

Output: 2018-1-16-5-02-0

Expected output: 2018-0-16-5-02-0

As per their docs month index start with zero but it doesn't work for me.

Moment version: ^2.20.1

Upvotes: 3

Views: 1765

Answers (2)

Alqama Bin Sadiq
Alqama Bin Sadiq

Reputation: 358

Try this moment(1516102320000).year() +"-"+ moment(1516102320000).month()+"-"+moment(1516102320000).day()

This is the way you can achieve your desired output. month() will return you 0 for January. format() will return 1 for January so in order to achieve this output 2018-0-2 you have to use this code moment(1516102320000).year() +"-"+ moment(1516102320000).month()+"-"+moment(1516102320000).day()

Upvotes: 2

Varun Sharma
Varun Sharma

Reputation: 1752

The format method will display months from 1 through 12. See this link for more info about format. The input is 0 indexed. If you specifically need 0 indexed months, you can use moment.month

Upvotes: 3

Related Questions