3

How do I create a date from a non-English language?

When using an English formatted string, the date returns a valid date.

var value = "3 mei, 2017 - 11:36";
//var format = 'd MMMM, yyyy - hh:mm';
var d = new Date(value);

When using a non-English formatted string, the date returns an invalid date.

var value = "3 may, 2017 - 11:36";
//var format = 'd MMMM, yyyy - hh:mm';
var d = new Date(value);

What should I change to my code to make the second version run?

Peter Raeves
  • 516
  • 1
  • 4
  • 17
  • @RobG This is obviously not a duplicate of the linked question. I removed the EEEE part, because it was not really relevant to the question. – Peter Raeves Apr 03 '17 at 12:51
  • The accepted answer there answers your question: don't expect the built-in Date parser to parse any string other than ISO 8601 format strings, and even then, older browsers (circa IE 8) will fail. – RobG Apr 03 '17 at 12:53
  • Why do people keep referring to the 'toLocaleString' function? I am trying to create the date, not output it. I want to use my dutch string to create the date. – Peter Raeves Apr 03 '17 at 12:53
  • As far as I know 'd MMMM, yyyy - hh:mm' is part of ISO 8601. Isn't it? It parses just fine when using English. – Peter Raeves Apr 03 '17 at 12:55
  • No, it's not. ECMAScript only supports a limited [*subset of ISO 8601*](http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date-time-string-format), not the whole thing. A couple of other formats are supported by convention (e.g. US m/d/yyyy and RFC2822), all of which is explained at [*MDN: Date.parse*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) (noting that the Date constructor and Date.parse are equivalent for parsing). BTW `new Date("3 may, 2017 - 11:36")` returns an invalid date in Safari 10.0.3. – RobG Apr 03 '17 at 13:01

1 Answers1

-4

I prefer you to use moment.js library.

https://momentjs.com/