Moment.js parsing text as date doesn't match language
String 12.01.2015
for example. in Germany - January 12, 2015. But the following code doesn't work as expected:
moment.locale('de');
moment('12.01.2015').toString(); // "Tue Dec 01 2015 00:00:00 GMT+0100"
moment('12.01.2015').fromNow(); // "in einem Jahr" (==> locale setting is OK)
Locale / de.js has the following:
longDateFormat : {
...
L : 'DD.MM.YYYY',
...
}
Why is the line not being processed as it seems to me?
+3
source to share
2 answers
The Momen't locale sets the desired moment output . Not an entrance. You will need to specify the input format like this:
moment('12.01.2015', 'DD.MM.YYYY')
See this github page for more on how this will change in the future.
You can wrap this in a function so you don't have to carry the format with you:
function germanMoment(date){
return moment(date, 'DD.MM.YYYY')
}
And then you can just use germanMoment('12.01.2015').fromNow()
which will work as desired.
+3
source to share
Moments the parsing method checks the ISO 8601 string. You must specify the date format.
moment("12.01.2015", "DD.MM.YYYY")
+2
source to share