javascriptdateunix-timestamp

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


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.


Solution

  • var now = new Date();
    var startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    var timestamp = startOfDay / 1000;