为什么在现在的Unix时间戳上加24h值的秒数不给我第二天?[已关闭]

问题描述 投票:0回答:1
let todaySeconds = Date.now();
//1591238661657
let todayDate = new Date(todaySeconds);
//Thu Jun 04 2020 05:44:21 GMT+0300 (Eastern European Summer Time

//3600 * 24 = 86400s in a day
console.log(new Date(todaySeconds + 86400));
//Thu Jun 04 2020 05:45:48 GMT+0300 (Eastern European Summer Time)

为什么上面不是2020年06月05日?

javascript unix-timestamp
1个回答
1
投票

JavaScript的时间戳以毫秒为单位,而不是以秒为单位。你必须添加 24 * 60 * 60 * 1000.

let todayMillis = Date.now();
let todayDate = new Date(todayMillis);

console.log(todayDate);
console.log(new Date(todayMillis + (24 * 60 * 60 * 1000)));

0
投票

你假设你的工作是以秒为单位。你是在用毫秒工作。请看 https:/developer.mozilla.orgen-USdocsWebJavaScriptReferenceGlobal_ObjectsDatenow。

The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
© www.soinside.com 2019 - 2024. All rights reserved.