Haris Bjelic Haris Bjelic - 16 days ago 7x
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

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

If I run this I got this output


instead of

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

Will return

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

How can I get the correct Date?


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