Haris Bjelic Haris Bjelic - 1 month ago 9
AngularJS Question

moment.js uses the month as day instead the day

I'm using currently moment.js (moment-with-locales.js - v.2.14.1) in my project. I want to remove the time of my datetime string to get only the date. But if I use the

.format()
method of moment.js I got an incorrect date.

I want to format this datetime string:

from ' 08.10.2016 11:00 ' to ' 08.10.2016 '

Here is a snipped that I used in my angular project:

var date = moment('08.10.2016 11:00').format('DD.MM.YYYY')
console.log(date)


If I run this I got this output

10.08.2016


instead of
08.10.2016

The funny thing is, if I want to get the timestamp (milliseconds) of my datetime string, it works perfect. Example:

var dateStart = moment('08.10.2016 19:00', 'DD.MM.YYYY HH:mm').valueOf()
console.log(dateStart)


Will return

1475946000000 -> Sat Oct 08 2016 19:00:00 GMT+0200


How can I get the correct Date?

Answer

It depends on your locale. en-US locate means moment will parse by "month day year". So, you need to parse with the pattern as well:

var date = moment('08.10.2016 11:00','DD.MM.YYYY HH:mm').format('DD.MM.YYYY')