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