Michael Joseph Aubry Michael Joseph Aubry - 11 months ago 41
Javascript Question

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')

Gives me
, obviously May should be
, 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?


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

enter image description here


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>