Michael Joseph Aubry
Michael Joseph Aubry

Reputation: 13492

Moment not formatting this date May 04, 2016

The server is giving me a date format like this May 04, 2016

var month = moment('May 04, 2016', 'MM-DD-YYYY').format('M')

month Gives me 4, obviously May should be 5, it's giving me the day it seems, and not the month.

How do I format the date the server gives me to determine the month using moment.js?

EDIT:

Not really sure why I am downvoted I actually tried the answer below and still get

enter image description here

Upvotes: 0

Views: 123

Answers (3)

Ernesto Troncoso
Ernesto Troncoso

Reputation: 1

try with

var month = moment('May 04, 2016', 'MMM-DD-YYYY').format('MM')

Change "MM" to "MMM" and .format('M') to format('MM').

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075785

When you give moment two string arguments, the documentation says the second is meant to be the format of the first. MM-DD-YYYY is not the format of May 04, 2016. The format of May 04, 2016 would be MMM DD, YYYY:

console.log(moment("May 04, 2016", "MMM DD, YYYY").format("M"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>

Upvotes: 4

SenthilKumarM
SenthilKumarM

Reputation: 119

According to documentation, the months ranges from 0 to 11.

So for May, it would return 4.

Reference : http://momentjs.com/docs/#/get-set/month/

Upvotes: -1

Related Questions