简体   繁体   中英

How can I get todays timestamp at 4pm?

I am looking for a way to receive the timestamp of today's 4pm in JavaScript.

When I use this code

Math.floor(Date.now() / 1000);

it give me the current timestamp. How can I specify just the hour to be static, the rest to rely on the current day?

Try:

var date = new Date();
date.setHours(16);
date.setMinutes(0);
date.setSeconds(0);
Math.floor(date.getTime() / 1000);

Here is the code to do that:

var d = new Date();//stores current date time

d.setHours(16);//4pm = 16 in 24 hour time

Now when you'll type console.log(d), you'll get the following required result. Tue May 12 2015 20:58:24 GMT+0500 (PKT)

You can do this by getting the current DateTime and change the hour, code:

var d = new Date();
d.setHours(16,0,0,0);//The setHours method can take optional minutes, seconds and ms arguments, but you can also do setHours(16)
Math.floor(d / 1000);

Instantiate a Date object and set the hours like so:

var d = new Date;
d.setHours(16);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM