javascript 你如何在javascript中获得今天开始的unix时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7195513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get the unix timestamp for the start of today in javascript?
提问by Anthony Hyman
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
.
我想要的是当天开始时的时间戳。例如,当前时间戳大致是1314297250
,我希望能够生成的是1314230400
which is the begin of today August 25th 2011
。
Thanks for your help.
谢谢你的帮助。
回答by Tim Down
var now = new Date();
var startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var timestamp = startOfDay / 1000;
回答by Luis Fontes
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
其中 86400 是一天的秒数
回答by pimvdb
var now = new Date; // now
now.setHours(0); // set hours to 0
now.setMinutes(0); // set minutes to 0
now.setSeconds(0); // set seconds to 0
var startOfDay = Math.floor(now / 1000); // divide by 1000, truncate milliseconds
回答by Alex Turpin
var d = new Date();
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
var t = d / 1000;
回答by Joseph Marikle
Another alternative for getting the beginning of the day is the following:
开始一天的另一种选择如下:
var now = new Date();
var beginningOfDay = new Date(now.getTime() -
now.getHours() * 60 * 60 * 1000 -
now.getMinutes() * 60 * 1000 -
now.getSeconds() * 1000 -
now.getMilliseconds());
回答by Henry Brewer
Luis Fontes' solution returns UTC timeso it can be 1 hour (daylight saving time) different from setHours solution.
Luis Fontes 的解决方案返回UTC 时间,因此它可能与 setHours 解决方案相差 1 小时(夏令时)。
var d = new Date();
var t = d - (d % 86400000);
Simplified version of examples above (local time).
上述示例的简化版本(当地时间)。
var d = new Date();
d.setHours(0,0,0,0);
var t = d / 1000;
Here you can find some performance tests: http://jsperf.com/calc-start-of-day
在这里你可以找到一些性能测试:http: //jsperf.com/calc-start-of-day
回答by digitalWestie
Alternatively you could subtract the modulo of a days length in miliseconds e.g.
或者,您可以减去以毫秒为单位的天长度的模数,例如
var day = 24*60*60*1000;
var start_of_today = Date.now() - Date.now() % day;
回答by Alexander
For any date it's easy to get Timestamps of start/end of the date using ISO String of the date ('yyyy-mm-dd'):
对于任何日期,使用日期的 ISO 字符串 ('yyyy-mm-dd') 可以轻松获取日期开始/结束的时间戳:
var dateString = '2017-07-13';
var startDateTS = new Date(`${dateString}T00:00:00.000Z`).valueOf();
var endDateTS = new Date(`${dateString}T23:59:59.999Z`).valueOf();
To get ISO String of today you would use (new Date()).toISOString().substring(0, 10)
So to get TS for today:
要获得今天的 ISO 字符串,您将使用(new Date()).toISOString().substring(0, 10)
So 来获得今天的 TS:
var dateString = (new Date()).toISOString().substring(0, 10);
var startDateTS = new Date(`${dateString}T00:00:00.000Z`).valueOf();
var endDateTS = new Date(`${dateString}T23:59:59.999Z`).valueOf();
回答by Venkat Kotra
var now = new Date();
var startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var timestamp = startOfDay.getTime() / 1000;
回答by Vu Anh
var yoursystemday = new Date(new Date().getTime()-(120000*60+new Date().getTimezoneOffset()*60000));
yoursystemday = new Date();
var current_time_stamp = Math.round(yoursystemday.getTime()/1000);