簡體   English   中英

您如何在 Javascript 中獲得今天結束的 Unix 時間戳?

[英]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;
  1. 您可以使用.valueOf()方法從日期獲取毫秒時間戳,而不是date / 1000
  2. 要結束一天,只需取第二天的日期並減少 1 毫秒

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM