Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

75%
+4 −0
Q&A Why is this client code getting the wrong date for a few hours a day?

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...

posted 3y ago by Monica Cellio‭  ·  edited 3y ago by Monica Cellio‭

Answer
#3: Post edited by user avatar Monica Cellio‭ · 2021-06-03T17:44:15Z (over 3 years ago)
removed wrong part and pointed to the answer with the correct explanation - thanks!
  • 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);
  • }
  • Hours are 0-based in `Date`, so 20 is 9PM (21:00), not 8PM. 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).
  • [Done in by time zones...](https://xkcd.com/1883/)
  • 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](https://software.codidact.com/posts/282006#answer-282006). 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).
  • [Done in by time zones...](https://xkcd.com/1883/)
#2: Post edited by user avatar Monica Cellio‭ · 2021-06-03T01:21:19Z (over 3 years ago)
  • 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);
  • }
  • Hours are 0-based in `Date`, so 20 is 9PM (21:00), not 8PM. 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).
  • [Done in by time zones...](https://xkcd.com/1883/)
  • 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);
  • }
  • Hours are 0-based in `Date`, so 20 is 9PM (21:00), not 8PM. 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).
  • [Done in by time zones...](https://xkcd.com/1883/)
#1: Initial revision by user avatar Monica Cellio‭ · 2021-06-03T01:20:20Z (over 3 years ago)
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);
    }

Hours are 0-based in `Date`, so 20 is 9PM (21:00), not 8PM.  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).

[Done in by time zones...](https://xkcd.com/1883/)