![](/img/trans.png)
[英]How do you get the unix timestamp for the start of today in javascript?
[英]How do you get the Unix timestamp for the end of today in Javascript?
我想得到今天結束的 Unix 時間戳。
使用此獲取今天的開始時間戳,但我想要今天的結束時間戳
var now = new Date();
var startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var timestamp = startOfDay / 1000;
.valueOf()
方法從日期獲取毫秒時間戳,而不是date / 1000
var now = new Date(); var startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()); // take next day date and reduce for one millisecond var endOfDay = new Date(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1) - 1); console.log({ startOfDay, endOfDay, startOfDayTimestamp: startOfDay.valueOf(), endOfDayTimestamp: endOfDay.valueOf() })
首先,獲取今天的時間戳:
var now = new Date();
然后得到明天的開始:
var startOfTomorrow = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDay() + 1);
現在你已經結束了今天:
var timestampOfEndOfDay = (startOfTomorrow - 1);
查看結果:
console.log("End of the Day", new Date(timestampOfEndOfDay) );
864e5
是一天中的毫秒數(24 * 60 * 60 * 1000), now % 864e5
是日期的時間部分,以毫秒為單位:
var now = new Date var startOfDay = new Date(now - now % 864e5) var endOfDay = new Date(now - now % 864e5 + 864e5 - 1) console.log(now) console.log(startOfDay) console.log(endOfDay)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.