KateMak
KateMak

Reputation: 1649

Cannot convert localized moment.js string to unix time, or any format

I am trying to convert a localized datetime string to unix time in moment.js, but to no avail. Does anyone know a workaround to do this?

moment.locale('de');
var a = moment('20.06.2015').format("X"); //returns Invalid Date
var b = moment('20.06.2015').unix(); //returns Invalid Date

Upvotes: 1

Views: 1351

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241959

Always supply a format when you parse.

moment.locale('de');
var a = moment('20.06.2015', 'DD.MM.YYYY').unix();

If you want, you can use the locale-defined formats. For German, the L format is DD.MM.YYYY.

moment.locale('de');
var a = moment('20.06.2015', 'L').unix();

This is useful if you might change locales and want the format to change to match.

Upvotes: 1

Related Questions