I am looking for a way to receive the timestamp of today's 4pm in JavaScript.
我正在寻找一种方法来接收今天下午4点的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?
它给我当前的时间戳。我怎样才能指定静止的小时,其余的依赖于当天?
4 个解决方案
#1
You can do this by getting the current DateTime and change the hour, code:
您可以通过获取当前DateTime并更改小时来执行此操作,代码:
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);
#2
Here is the code to do that:
这是执行此操作的代码:
var d = new Date();//stores current date time
var d = new Date(); //存储当前日期时间
d.setHours(16);//4pm = 16 in 24 hour time
d.setHours(16); // 4pm = 16小时24小时
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)
现在,当您键入console.log(d)时,您将获得以下所需结果。 2015年5月12日星期二20:58:24 GMT + 0500(PKT)
#3
Try:
var date = new Date();
date.setHours(16);
date.setMinutes(0);
date.setSeconds(0);
Math.floor(date.getTime() / 1000);
#4
Instantiate a Date
object and set the hours like so:
实例化Date对象并设置小时,如下所示:
var d = new Date;
d.setHours(16);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours
#1
You can do this by getting the current DateTime and change the hour, code:
您可以通过获取当前DateTime并更改小时来执行此操作,代码:
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);
#2
Here is the code to do that:
这是执行此操作的代码:
var d = new Date();//stores current date time
var d = new Date(); //存储当前日期时间
d.setHours(16);//4pm = 16 in 24 hour time
d.setHours(16); // 4pm = 16小时24小时
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)
现在,当您键入console.log(d)时,您将获得以下所需结果。 2015年5月12日星期二20:58:24 GMT + 0500(PKT)
#3
Try:
var date = new Date();
date.setHours(16);
date.setMinutes(0);
date.setSeconds(0);
Math.floor(date.getTime() / 1000);
#4
Instantiate a Date
object and set the hours like so:
实例化Date对象并设置小时,如下所示:
var d = new Date;
d.setHours(16);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours