Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

momentjs parse german date string

I'm trying to parse the String using momentjs:

let date = "19. Juli 2016 05:00";

The string contains the month of july written in german and it has the "LLL" format according to the momentjs doc.

When I try to parse it with

moment(date,'LLL');

I get a wrong date.

When I try to parse it with

moment(date,'LLL','de');

I get the error message:

TypeError: config._locale is null.

How can I correctly parse this string?

like image 996
Anh Tuan Nguyen Avatar asked Jul 19 '16 08:07

Anh Tuan Nguyen


1 Answers

You should use corresponding moment-locales dependency to be able to recognize dates in other languages. You can find the list of locales here and use it from the CDN, or download and bundle it yourself from momentjs (get the one that says moment-with-locales.js)

And in that case use the following parse method (as you've tried it already):

let momentObj = moment(DATE_STRING, FORMAT, LOCALE);

Demo

let momentObj = moment("04. Juli 2016 05:00", 'LLL', 'de');
console.log(momentObj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/locale/de.js"></script>
like image 69
Matyas Avatar answered Sep 29 '22 06:09

Matyas