views:

37

answers:

2

How do I convert 07/26/2010 to a UNIX timestamp using Javascript?

+3  A: 

Take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp

There is a function UTC() that returns the milliseconds from the unix epoch.

webdestroya
+1 just remember to divide by `1000` for `ms -> s`.
Anurag
+2  A: 

You can create a Date object, and call getTime on it:

new Date(2010, 6, 26).getTime() / 1000
Michael Mrozek
The month argument for the date constructor lacks a little consistency and is actually zero-based. This means 7 is August, so you need to subtract 1 :-)
Andy E
@Andy - it is a whacky idea to offset months by -1 while keeping the rest, and is exactly the kind of thing that can make a space shuttle go nuts, if JavaScript were ever to be used there :)
Anurag
@Andy That's...wow. Fixed, thanks
Michael Mrozek
Probably better to go: new Date("2010-7-26").getTime() / 1000 so you don't have to think about offsets.
Vincent McNabb
@Vincent: `new Date("2010-7-26")` will not parse in Internet Explorer. *Date* constructor relies on the *Date.parse()* method when a string is passed, which is implementation dependant in ECMA-262 3rd edition and, as a result, notoriously inflexible in IE.
Andy E