Javascript Unixtime

Just a note of how to get Unixtime in Javascipt.

var date_obj    = new Date;
var unixtime_ms = date_obj.getTime();
var unixtime    = parseInt(unixtime_ms / 1000);

Also a Javascript Unixtime conversion utility.

Tags:
Leave A Reply - 11 Replies
Replies
J January 25th 2009

Thanks for this snippet. You saved me a lot of time.

michael February 15th 2009

yeah thx mate

Andy Moore October 16th 2009

Brilliant, thanks. Just what I was looking for.

AC January 12th 2010

Great little script. Thanks for sharing

Xander May 3rd 2010

Great example ! keep it simple and everyone understand

Kelson September 12th 2010

thx, exactly what I need.

Andrew September 23rd 2010

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 December 11th 2010

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 December 11th 2010

Also, valueOf (as per Andrew's comment) is slightly faster than getTime:

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

soupKitchen September 6th 2012

Thanks man! Saved me TONS(!!!) of time.

ximik777 June 11th 2014

parseInt(+new Date() / 1000);

All content licensed under the Creative Commons License