Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 979
Anthony Jack Avatar asked Aug 25 '11 18:08

Anthony Jack


People also ask

How do I get the current UNIX timestamp?

To find the unix current timestamp use the %s option in the date command. The %s option calculates unix timestamp by finding the number of seconds between the current date and unix epoch.

How do you get a timestamp in JavaScript?

In JavaScript, in order to get the current timestamp, you can use Date. now() . It's important to note that Date. now() will return the number of milliseconds since January, 1 1970 UTC.

What is UNIX timestamp in JavaScript?

The getTime method returns the number of milliseconds since the Unix Epoch (the 1st of January, 1970 00:00:00). If you need to convert the result to seconds, divide it by 1000 .

How do you get UNIX timestamp in seconds JavaScript?

To get the unix timestamp using JavaScript you need to use the getTime() function of the build in Date object. As this returns the number of milliseconds then we must divide the number by 1000 and round it in order to get the timestamp in seconds. Math. round(new Date().


2 Answers

var now = new Date(); var startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()); var timestamp = startOfDay / 1000; 
like image 191
Tim Down Avatar answered Sep 26 '22 15:09

Tim Down


Well, the cleanest and fastest way to do this is with:

long timestamp = 1314297250; long beginOfDay = timestamp - (timestamp % 86400); 

where 86400 is the number of seconds in one day

like image 27
Luis Fontes Avatar answered Sep 25 '22 15:09

Luis Fontes