Javascript Unixtime  

Just a note of how to get Unixtime in Javascipt.

var foo = new Date; // Generic JS date object var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch var unixtime = parseInt(unixtime_ms / 1000);
Also a Javascript Unixtime conversion utility.
Leave A Reply - 9 Replies
Replies
J 2009-01-25 01:22pm - No Email - Logged IP: 131.96.130.112
Thanks for this snippet. You saved me a lot of time.
michael 2009-02-15 10:08am - No Email - Logged IP: 77.254.242.181
yeah thx mate
Andy Moore 2009-10-16 09:16am - No Email - Logged IP: 91.84.153.162
Brilliant, thanks. Just what I was looking for.
AC 2010-01-12 11:13am - No Email - Logged IP: 18.62.12.27
Great little script. Thanks for sharing
Xander 2010-05-03 02:42am - No Email - Logged IP: 81.12.134.7
Great example !
keep it simple and everyone understand
Kelson 2010-09-12 04:09am - No Email - Logged IP: 92.106.255.208
thx, exactly what I need.
Andrew 2010-09-23 06:29am - No Email - Logged IP: 124.188.51.145
Use multiplication over division, it's much faster.
Also, Math.Floor is faster than parseInt
var unixtime = Math.Floor(unixtime_ms.valueOf() * 0.001);
Ian 2010-12-11 03:41pm - No Email - Logged IP: 80.229.16.135
Use the optimizations Andrew gave. Plus, faster still than Math.floor is:

var unixtime = (new Date().getTime() * 0.001)|0;

Because it removes the extra namespace lookup and function call (see Ecmascript spec 11.10 - the behavior is explicitly specified).
Ian 2010-12-11 03:44pm - No Email - Logged IP: 80.229.16.135
Also, valueOf (as per Andrew's comment) is slightly faster than getTime:

var unixtime = (new Date().valueOf() * 0.001)|0;
All content licensed under the Creative Commons License