views:

694

answers:

4

i have a string which, i want to compare with a javascript datetime object. how to convert string "1/1/1912" into datetime using JavaScript so that i can compare like

if (EDateTime > ('1/1/1912'))  {...}
A: 

Convert your string to timestamp with Date object.

I found something like:

function toTimestamp(year,month,day,hour,minute,second){
   var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
   return datum.getTime()/1000;
}

Year, month and day parts get with regular expressions.

hsz
+1  A: 

How about using DateJS library?

It can convert 1/1/1912 to Monday, January 01, 1912 12:00:00 AM easily

S.Mark
A: 

You could do this simply with a split if you can guarantee the date format.

var dateArray = '1/1/1912'.split("/");
new Date(dateArray[2], dateArray[1], dateArray[0]);
slashnick
A: 

This library may be helpful.

altCognito