Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Why is this client code getting the wrong date for a few hours a day?
Parent
Why is this client code getting the wrong date for a few hours a day?
Our web site has a widget that displays some date-based information that we retrieve from some JSON-formatted data (one object per date). We get the current date from the environment, possibly adjust it (see below), look up the right entry in the JSON, and display it. Most of the time this works right, but at certain times of day we see an off-by-one error.
About that adjustment: For purposes of our widget, the day does not start at midnight but at 8PM local time. (The widget is for an alternate calendar where the day starts at sunset. 8PM is a compromise because we are not going to look up actual sunset times in the client locale.) We therefore get the local date and time, and if it's after 8PM we increment the date before looking up the entry.
The behavior I have been seeing is:
- From midnight to about 8PM (my time), the information is correct.
- At 8PM the day rolls over to "tomorrow" as expected.
- At 9PM the day rolls over again, so it is now really wrong (one full day ahead of where it should be).
- At midnight it rolls back and is correct.
According to the documentation, the Javascript Date
class operates in local time so what we're doing should work fine. The objects in the JSON use YYYY-MM-DD date format and Date
doesn't by default, so we use toISOString()
to convert the local date into this format:
let now = new Date();
if (now.getHours() > 20) {
now.setDate(now.getDate() + 1);
}
now = now.toISOString().substr(0, 10);
// use that to look up the value
Why is it incrementing twice, making the value wrong for a few hours a day?
Post
There are two issues here. The first is that this code isn't rolling over at 8PM as expected:
if (now.getHours() > 20) {
now.setDate(now.getDate() + 1);
}
This change happens at 9PM as explained in this answer. The second increment is the one that's coming from this code, not the first one.
This leaves the question of what's going on at 8PM. The answer is that while Date
uses local time if you get individual elements like day or hour (or even toString()
), the ISO standard isn't just about getting YYYY-MM-DD formatting. ISO 8601 uses the UTC date. It's shortly before 9PM local time as I write this, and this is what we get from various operations:
let now = new Date();
now.getDate(); // returns 2
now.getHours(); // returns 20
now.toISOString(); // returns 2021-06-03T00:50:44.053Z
My time zone is UTC-4. Over in UTC-0, aka "Z" time, it's no longer June 2 but has rolled over to June 3. This happens at 8PM. That's where the first rollover is coming from.
In order to get the local date in the YYYY-MM-DD format, you have to use getFullYear()
, getMonth()
, and getDate()
and assemble the string yourself. If month or date is one digit you have to add the leading 0, and don't forget to increment the month (months are 0-based).
1 comment thread