gpt4 book ai didi

与新日期相比,javascript getTime() 为旧日期返回更大的值

转载 作者:行者123 更新时间:2023-11-30 12:12:33 24 4
gpt4 key购买 nike

javascript getTime() 返回从 1970 年 1 月 1 日午夜算起的毫秒数和日期对象中的时间值。但是,

  new Date('Wed Sep 16 2105 05:30:00 GMT+0530').getTime()
// returns 4282502400000
new Date('Tue Oct 26 2015 05:30:00 GMT+0530').getTime()
// returns 1445817600000

后者(Tue Oct 26 2015 05:30:00 GMT+0530)返回的值不应该更大。我想找到给定日期(通知时间戳)和今天之间的列表日期。我编写下面的代码时假设 getTime() 为旧日期返回的值总是小于新日期。

    var timestamp = new Date('9/15/2105, 12:00:00 AM').getTime();
var startDate = new Date(timestamp);
// Date.UTC() to avoid timezone and daylight saving
var date = new Date(Date.UTC(startDate.getFullYear(),
startDate.getMonth(),
startDate.getDate()
));
var currentDay = new Date();
var currentDayTimestamp = new Date(Date.UTC(currentDay.getFullYear(),
currentDay.getMonth(),
currentDay.getDate()
)).getTime();
// day in millisec, 24*60*60*1000 = 86400000
date = new Date(date.getTime() + 86400000);
var dates = [];
console.info(date + ' : ' + date.getTime());
console.info(new Date(currentDayTimestamp) + ' : ' + currentDayTimestamp);
while(date.getTime() <= currentDayTimestamp) {
var dateObj = {
date: date.getUTCDate(),
month: date.getUTCMonth() + 1,
year: date.getUTCFullYear()
}
dates.push(dateObj);
date = new Date(date.getTime() + 86400000);
}
console.info(JSON.stringify(dates));

OUTPUT:
Wed Sep 16 2105 05:30:00 GMT+0530 (IST) : 4282502400000
Tue Oct 27 2015 05:30:00 GMT+0530 (IST) : 1445904000000
[]

最佳答案

问题是您的日期输入错误。一个是 2105 年,它比 2015 年大得多。

关于与新日期相比,javascript getTime() 为旧日期返回更大的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33361522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com