How do I convert 07/26/2010
to a UNIX timestamp using Javascript?
views:
37answers:
2
+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
2010-07-29 22:19:34
+1 just remember to divide by `1000` for `ms -> s`.
Anurag
2010-07-29 22:26:23
+2
A:
You can create a Date
object, and call getTime
on it:
new Date(2010, 6, 26).getTime() / 1000
Michael Mrozek
2010-07-29 22:19:42
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
2010-07-29 22:25:08
@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
2010-07-29 22:27:59
Probably better to go: new Date("2010-7-26").getTime() / 1000 so you don't have to think about offsets.
Vincent McNabb
2010-07-29 22:37:06
@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
2010-07-30 07:50:20