0

I have the following date format = 16 September 2016

This example isnt a good one because the month happens to be the same in English. But some other months are in the german language. How can i convert this to a date?

I have an idea of creating a static object with months and their corresponding values and it would work like that. Is this the only way or is there a better one.

My idea: dateObj = {januar : 01 , februar : 02 , ... } etc Then i could do a lookup and find the value i need.

Kevin.a
  • 4,094
  • 8
  • 46
  • 82
  • 1
    Remember that if you are going to convert to a number and lookup the month, that getMonth() is a zero-based array in JS (e.g. September is 08) – ecg8 Nov 06 '19 at 16:27
  • Possible duplicate of [How To Convert A Foreign Month In A Date String Into English](https://stackoverflow.com/questions/12591985/how-to-convert-a-foreign-month-in-a-date-string-into-english) – inorganik Nov 06 '19 at 16:29

1 Answers1

0

I would do a string replace for the translation (eventually after converting toLowerCase() ) to make sure you have it in a standard format, and then use the Date.parse() method to convert.

var str = "16 Oktober 2016";
var mapObj = {
    'januar':'January',
    'februar':'February',
    'märz':'March',
    'marz':'March',
    'april':'April',
    'mai':'May',
    'juni':'June',
    'juli':'July',
    'august':'August',
    'september':'September',
    'oktober':'October',
    'november':'November',
    'dezember':'December',
};
str = str.replace(/januar|februar|märz|marz|april|mai|juni|juli|august|september|oktober|november|dezember/gi, function(matched){
  return mapObj[matched.toLowerCase()];
});
var d = Date.parse(str);

Based on the good example for the string replace: Replace multiple strings with multiple other strings

Telmo Dias
  • 3,938
  • 2
  • 36
  • 48
  • "16 September 2016" is not a format supported by ECMA-262 (see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results)). It would be better to parse the month name to a number and use the constructor as `new Date(2016, 9, 16)`. Also, *Date.parse* doesn't return a date. – RobG Nov 06 '19 at 20:25
  • In any of my browsers, Date.parse('16 September 2016') converts to 1473980400000 epoch which as you can test here https://www.epochconverter.com/ converts back to Friday, 16 September 2016 depending on the timezone. Of course you can elaborate more on this. The purpose is not to write the needed code, but instead to provide a way to do what the operator asked help for. – Telmo Dias Nov 06 '19 at 20:41
  • The issue is that your strategy is to parse the string, create another string that isn't supported by ECMA-262, then parse it with the built–in parser. That's not a good strategy as it relies on the built–in parser and an unsupported format. If you convert the month name to a number, then you can pass the values for year, month and day directly to the Date constructor. This avoids both creating another string and using the built–in parser, so far more reliable (and less code). ;-) – RobG Nov 07 '19 at 03:21
  • Seems also a reasonable solution, but I would suggest you write a proper example. ;-) – Telmo Dias Nov 07 '19 at 08:05