猿问

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

我正在制作一个用于根据当前时间重新计算时间的函数。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。


我错过了什么吗?谢谢


慕标5832272
浏览 163回答 3
3回答

慕码人8056858

您似乎假设在setDate不更改原始日期的情况下创建了一个新日期。它不是。相反,它只是修改日期对象并返回一个自引用。的用法是这样的:&nbsp;&nbsp;date.setDate(10); &nbsp;&nbsp;date.setMinutes(10);可以更优雅地写成:&nbsp;&nbsp;date.setDate(10).setMinutes(10);如果要创建两个独立的日期,则必须复制日期:&nbsp;&nbsp;const&nbsp;copy&nbsp;=&nbsp;new&nbsp;Date(oldDate);

月关宝盒

请注意setDate会修改您的引用,而不会创建新引用。&nbsp; &nbsp; let timeNow = new Date();&nbsp; &nbsp; let timeShowToday = new Date(&nbsp; &nbsp;//set at 8:00pm everyday&nbsp; &nbsp; &nbsp; timeNow.getFullYear(),&nbsp; &nbsp; &nbsp; timeNow.getMonth(),&nbsp; &nbsp; &nbsp; timeNow.getDate(),&nbsp; &nbsp; &nbsp; 20,&nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; let timeShowYesterday = new Date(timeShowToday);&nbsp; &nbsp; timeShowYesterday.setDate(timeShowYesterday.getDate() - 1);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; console.log('Yesterday', timeShowYesterday);&nbsp; &nbsp; console.log('Today', timeShowToday);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; let timeDiff = timeShowToday.getTime() - timeNow.getTime();&nbsp; &nbsp; if (timeDiff < 0) {&nbsp; //recalculate if current Time is past specific time(8:00pm)&nbsp;&nbsp; &nbsp; &nbsp; const timeShowYesterday = new Date(&nbsp; &nbsp; &nbsp; &nbsp; timeNow.getFullYear(),&nbsp; &nbsp; &nbsp; &nbsp; timeNow.getMonth(),&nbsp; &nbsp; &nbsp; &nbsp; timeNow.getDate(),&nbsp; &nbsp; &nbsp; &nbsp; 20,&nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; timeShowToday = new Date(timeShowYesterday);&nbsp; &nbsp; &nbsp; timeShowToday.setDate(timeShowToday.getDate() + 1);&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;console.log('Yesterday', timeShowYesterday);&nbsp; &nbsp; &nbsp; &nbsp;console.log('Today', timeShowToday);&nbsp; &nbsp; }我更新了let timeShowYesterday = new Date(timeShowToday);&nbsp;&nbsp;timeShowYesterday.setDate(timeShowYesterday.getDate() - 1);和timeShowToday = new Date(timeShowYesterday);&nbsp;&nbsp;timeShowToday.setDate(timeShowToday.getDate() + 1);为避免覆盖您的参考,只需创建一个新日期并进行更新即可。

12345678_0001

发现了问题!我以为setDate&nbsp;timeShowToday&nbsp;=&nbsp;timeShowYesterday.setDate( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeShowYesterday.getDate()&nbsp;+&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);会从timeShowYesterday创建一个新实例,而不影响它,并将其分配给timeShowToday,但实际上对其进行了修改。所以+1是问题所在。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答