我正在制作一个用于根据当前时间重新计算时间的函数。timeShowToday是每天的特定时间(设置为8:00 pm),以显示一些盲目的答案。
_checkTime = () => {
let timeNow = new Date();
let timeShowToday = new Date( //set at 8:00pm everyday
timeNow.getFullYear(),
timeNow.getMonth(),
timeNow.getDate(),
20,
0
);
let timeShowYesterday = timeShowToday.setDate(timeShowToday.getDate() - 1);
let timeDiff = timeShowToday.getTime() - timeNow.getTime();
if (timeDiff < 0) { //recalculate if current Time is past specific time(8:00pm)
let temp = new Date(
timeNow.getFullYear(),
timeNow.getMonth(),
timeNow.getDate(),
20,
0
);
console.log(temp);
timeShowYesterday = temp;
timeShowToday = timeShowYesterday.setDate(
timeShowYesterday.getDate() + 1
);
console.log(timeShowYesterday);
}
这里的问题是,尽管我刚刚将temp分配给timeShowYesterday,但变量temp和timeShowYesterday的日期时间却不同。这是在我进行控制台日志时得到的日志:
05-03 00:26:59.623 ReactNativeJS:temp:星期五五月03 2019 20:00:00 GMT + 0900
05-03 00:26:59.623 ReactNativeJS:timeShowYesterday:星期六五月04 2019 20:00:00 GMT + 0900
如您所见,temp正确记录了当前时间,但是timeShowYesterday具有+1天。我不知道为什么会这样,因为我所做的只是将temp分配给timeShowYesterday。
我错过了什么吗?谢谢
慕码人8056858
月关宝盒
12345678_0001
相关分类