0

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.

_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. 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: timeShowYesterday: Sat May 04 2019 20:00:00 GMT+0900

As you can see, temp logs the current time correctly but timeShowYesterday has +1 day. I have no idea why this is the case, because all I did was just assign temp to timeShowYesterday.

am I missing something? Thank you

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
Eric Ahn
  • 391
  • 2
  • 7
  • 18
  • 2
    The calls to `.setDate()` *modify* the date you're operating on — they do not create a new Date instance. – Pointy May 02 '19 at 15:38
  • 1
    I see you are adding +1 `timeShowToday = timeShowYesterday.setDate( timeShowYesterday.getDate() + 1 );` – F.bernal May 02 '19 at 15:38
  • 2
    you are logging `temp` here `console.log(temp);` before you add one day to `timeShowYesterday`, so it shows previous value (because the log converts the object to a string instead of showing the actual object's reference) – Kaddath May 02 '19 at 15:38
  • Why do you need `temp` at all? – Jonas Wilms May 02 '19 at 15:41
  • @Pointy right, I thought it creates a new date instance. That is the mistake! Thank you very much guys for the quick help!!! – Eric Ahn May 02 '19 at 15:41
  • @JonasWilms i created it just to show that there is the difference between the two. I thought timeShowYesterday.setDate would create new instance but it modified it – Eric Ahn May 02 '19 at 15:42
  • Would like to upvote if anyone posts as an answer! – Eric Ahn May 02 '19 at 15:47

3 Answers3

2

Be aware about setDate modifies your reference no creates a new one.

    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.

F.bernal
  • 2,594
  • 2
  • 22
  • 27
2

You seem to assume that setDate creates a new date without changing the original date. 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);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

Found the problem!

I thought the 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.

So +1 was the problem.

Eric Ahn
  • 391
  • 2
  • 7
  • 18