I have a date in this format: dd.mm.yyyy
When I instantiate a JavaScript date with it, it gives me a NaN
In c# I can specify a date format, to say: here you have my string, it’s in this format, please make a Datetime of it.
Is this possible in JavaScript too? If not, is there an easy way?
I would prefer not to use a substring for day, substring for month etc. because my method must also be capable of german, italian, english etc. dates.
You will need to create a function to extract the date parts and use them with the
Dateconstructor.Note that this constructor treats months as zero based numbers (
0=Jan, 1=Feb, ..., 11=Dec).For example:
Edit: For handling a variable format you could do something like this:
The above function accepts a format parameter, that should include the
yyyymmandddplaceholders, the separators are not really important, since only digits are captured by the RegExp.You might also give a look to DateJS, a small library that makes date parsing painless…