Maxi M. Maxi M. - 4 months ago 17
Javascript Question

Date format with Moment.js or classic JS, change places of day and month?

I get date value from my source in this format:
02.08.2016 / Day.Month.Year, it's correct.

but JavaScript understand this inverse, Month.Day.Year.

I tried some ways with Moment.js like:

moment('15.08.2016').format('MM.DD.YYYY');


My JsFiddle is: http://jsfiddle.net/PAc3j/389/
There are some results visible, see please output, variable dateThree: Invalid date

My question is, how can I do it with Moment.js or classic JavaScript?

Answer

You can specify the format of the input date string like this.

moment('15.08.2016', 'DD.MM.YYYY').format('MM.DD.YYYY');

http://momentjs.com/docs/#/parsing/string-format