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.

Comments on Why is this client code getting the wrong date for a few hours a day?

Post

Why is this client code getting the wrong date for a few hours a day?

+7
−0

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (2 comments)
General comments
Monica Cellio‭ wrote over 3 years ago

It took me an embarrassingly long time to figure this out, so I'm leaving a trail here for others or for future-me.

Alexei‭ wrote over 3 years ago

Not an expert on the matter, but coming from .NET development area, native JS date/time manipulation looks like a mess. I would definitely consider using a library, if possible: https://terodox.tech/migrating-away-from-momentjs-part1/