How do you get the unix timestamp for the start of today in javascript?

Anthony Jack picture Anthony Jack · Aug 25, 2011 · Viewed 21.1k times · Source

I realize that the current timestamp can be generated with the following...

var timestamp = Math.round((new Date()).getTime() / 1000);

What I'd like is the timestamp at the beginning of the current day. For example the current timestamp is roughly 1314297250, what I'd like to be able to generate is 1314230400 which is the beginning of today August 25th 2011.

Thanks for your help.

Answer

Tim Down picture Tim Down · Aug 25, 2011
var now = new Date();
var startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var timestamp = startOfDay / 1000;