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
January 25th 2009 - J

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

February 15th 2009 - michael

yeah thx mate

October 16th 2009 - Andy Moore

Brilliant, thanks. Just what I was looking for.

January 12th 2010 - AC

Great little script. Thanks for sharing

May 3rd 2010 - Xander

Great example ! keep it simple and everyone understand

September 12th 2010 - Kelson

thx, exactly what I need.

September 23rd 2010 - Andrew

Use multiplication over division, it's much faster. Also, Math.Floor is faster than parseInt var unixtime = Math.Floor(unixtime_ms.valueOf() * 0.001);

December 11th 2010 - Ian

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).

December 11th 2010 - Ian

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

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

September 6th 2012 - soupKitchen

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

June 11th 2014 - ximik777

parseInt(+new Date() / 1000);

All content licensed under the Creative Commons License