Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Date String returns invalid Date

I am trying to convert a date string into a date object within javascript. My date has the following format:

"13.02.2015 12:55"

My current approach was:

var d = new Date("13.02.2015 12:55");

But this didnt work and always returns invalid date. If I enter a date as "12.02.2015 12:55" it works in chrome but not in firefox. I guess this is because he thinks the first part is the month, but in germany this is not the case.

How can I get this to work?

like image 279
zanzoken Avatar asked Oct 27 '25 10:10

zanzoken


1 Answers

use moment.js:

var date = moment("13.02.2015 12:55", "DD.MM.YYYY HH.mm").toDate();

Update 2022-05-28:

Meanwhile the project status of moment.js has changed. Therefore I strongly suggest to read https://momentjs.com/docs/#/-project-status/ and observe the recommendations.

like image 179
Remigius Stalder Avatar answered Oct 30 '25 01:10

Remigius Stalder