简体   繁体   English

关于JavaScript中的日期函数的问题

[英]Question regarding date function in javascript

I'm making a function for recalculating a time based on the current time. 我正在制作一个用于根据当前时间重新计算时间的函数。 timeShowToday is a specific time everyday (set at 8:00 pm) to reveal some blinded answer. 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);
    }

The problem here is that variable temp and timeShowYesterday has different datetime eventhough I've just assigned temp to timeShowYesterday. 这里的问题是,尽管我刚刚将temp分配给timeShowYesterday,但变量temp和timeShowYesterday的日期时间却不同。 This is the log I get when I console log it: 这是在我进行控制台日志时得到的日志:

05-03 00:26:59.623 ReactNativeJS: temp: Fri May 03 2019 20:00:00 GMT+0900 05-03 00:26:59.623 ReactNativeJS:临时:2019年5月3日星期五20:00:00 GMT + 0900
05-03 00:26:59.623 ReactNativeJS: timeShowYesterday: Sat May 04 2019 20:00:00 GMT+0900 05-03 00:26:59.623 ReactNativeJS:timeShow昨天:2019年5月4日星期六20:00:00 GMT + 0900

As you can see, temp logs the current time correctly but timeShowYesterday has +1 day. 如您所见,temp正确记录了当前时间,但是timeShowYesterday具有+1天。 I have no idea why this is the case, because all I did was just assign temp to timeShowYesterday. 我不知道为什么会这样,因为我所做的只是将temp分配给timeShowYesterday。

am I missing something? 我错过了什么吗? Thank you 谢谢

Be aware about setDate modifies your reference no creates a new one. 请注意setDate会修改您的参考,而不会创建新参考。

  let timeNow = new Date(); let timeShowToday = new Date( //set at 8:00pm everyday timeNow.getFullYear(), timeNow.getMonth(), timeNow.getDate(), 20, 0 ); let timeShowYesterday = new Date(timeShowToday); timeShowYesterday.setDate(timeShowYesterday.getDate() - 1); console.log('Yesterday', timeShowYesterday); console.log('Today', timeShowToday); let timeDiff = timeShowToday.getTime() - timeNow.getTime(); if (timeDiff < 0) { //recalculate if current Time is past specific time(8:00pm) const timeShowYesterday = new Date( timeNow.getFullYear(), timeNow.getMonth(), timeNow.getDate(), 20, 0 ); timeShowToday = new Date(timeShowYesterday); timeShowToday.setDate(timeShowToday.getDate() + 1); console.log('Yesterday', timeShowYesterday); console.log('Today', timeShowToday); } 

I updated 我更新了

let timeShowYesterday = new Date(timeShowToday); timeShowYesterday.setDate(timeShowYesterday.getDate() - 1);

and

timeShowToday = new Date(timeShowYesterday); timeShowToday.setDate(timeShowToday.getDate() + 1);

To avoid overwriting your refence, just create a new date and update it. 为避免覆盖您的参考,只需创建一个新日期并进行更新即可。

You seem to assume that setDate creates a new date without changing the original date. 您似乎假设setDate创建了一个新日期而不更改原始日期。 It does not. 它不是。 Instead it just modifies the date object and returns a self reference. 相反,它只是修改日期对象并返回一个自引用。

The usage of that is that this: 的用法是这样的:

  date.setDate(10);
  date.setMinutes(10);

can be written more elegantly as: 可以更优雅地写成:

  date.setDate(10).setMinutes(10);

If you want to create two independent dates, you have to copy the date : 如果要创建两个独立的日期,则必须复制日期

  const copy = new Date(oldDate);

Found the problem! 发现了问题!

I thought the setDate 我以为setDate

 timeShowToday = timeShowYesterday.setDate(
        timeShowYesterday.getDate() + 1
      );

would create a new instance from timeShowYesterday without affecting it and assign it to timeShowToday but actually modified it. 会从timeShowYesterday创建一个新实例,而不影响它,并将其分配给timeShowToday,但实际上对其进行了修改。

So +1 was the problem. 所以+1是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM