This:
const sunrise = getLocalDatetime(sys.date).setHours(5, 59);
Is the same as this:
const sunrise = new Date(sys.date).setHours(5, 59);
And in fact, I don’t think this actually does what you think it does:
const now = getLocalDatetime(sys.date).getTime();
Because setHours
already sets the local time, you don’t have to use getLocalDatetime()
to adjust for the timezone: the adjustment that you do now gets reverted by using setHours
. However, that reversion doesn’t happen with .getTime()
, which means that now
will actually be “now + X hours” (where X is the current timezone offset).
To demonstrate the issue, you can add this to your code:
const sunrise = getLocalDatetime(sys.date).setHours(5, 59);
const sunset = getLocalDatetime(sys.date).setHours(20, 16)
const now = getLocalDatetime(sys.date).getTime();
const _ = d => new Date(d).toString()
console.log('sunrise', _(sunrise), _(new Date(sys.date).setHours(5, 59)));
console.log('sunset ', _(sunset), _(new Date(sys.date).setHours(20, 16)));
console.log('now ', _(now), _(Date.now()));
For me, this displays the following:
sunrise Fri Aug 07 2020 05:59:20 GMT+0200 (Central European Summer Time) Fri Aug 07 2020 05:59:20 GMT+0200 (Central European Summer Time)
sunset Fri Aug 07 2020 20:16:20 GMT+0200 (Central European Summer Time) Fri Aug 07 2020 20:16:20 GMT+0200 (Central European Summer Time)
now Fri Aug 07 2020 13:57:20 GMT+0200 (Central European Summer Time) Fri Aug 07 2020 11:57:20 GMT+0200 (Central European Summer Time)
As you can see, both sunrise
and sunset
show the exact same strings, regardless of whether getLocalDatetime()
gets called or not.
You can also see that “your” value of now
is showing a timestamp 2 hours into the future (it’s 11:57 here at the moment).