How to convert date to timestamp?

selladurai picture selladurai · Mar 26, 2012 · Viewed 430.2k times · Source

I want to convert date to timestamp, my input is 26-02-2012. I used

new Date(myDate).getTime();

It says NaN.. Can any one tell how to convert this?

Answer

The Alpha picture The Alpha · Mar 26, 2012

Split the string into its parts and provide them directly to the Date constructor:

Update:

var myDate = "26-02-2012";
myDate = myDate.split("-");
var newDate = new Date( myDate[2], myDate[1] - 1, myDate[0]);
console.log(newDate.getTime());