Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string 2010-1-10 to date object in javascript?

The string can be got by getFullYear and so on.

Can it be done reversely?

like image 307
user198729 Avatar asked Jan 06 '10 14:01

user198729


1 Answers

Replace - by / and JavaScript can parse it. Stupid mistake in JS specification (ISO date/time standard is very clear that - is correct)

var str = "2010-1-10";
alert(Date.parse(str.replace(/-/g,"/")));

Try pasting it in your browser: javascript:alert(Date.parse("2010-01-01".replace(/-/g,"/")));

like image 79
MSalters Avatar answered Sep 29 '22 19:09

MSalters