Representative Line: In the Zone

Date:

Share:

Robert R picked up a bug in his company's event scheduling app. Sometimes, events were getting reported a day off from when they actually were.

It didn't take too long to find the culprit, and as is so often the case, the culprit was handling dates with strings.

const dateAsString = event.toISOString().substr(0,10);  
return new Date(dateAsString);

toISOString returns a "simplified" ISO8601 string, which looks like this: YYYY-MM-DDTHH:mm:ss.sssZ. The substr pops off the first ten characters, giving you YYYY-MM-DD.

The goal, as you can likely gather, is to truncate to just the date part of a date-time. And given that JavaScript doesn't have a convenient method to do that, it doesn't seem like a terrible way to solve that problem, if you don't think about what date-times contain too hard.

But there's an obvious issue here. toISOString always converts the date to UTC, converting from your local timezone to UTC. Which means when you pick off just the date portion of that, you may be off by an entire day, depending on the event's scheduled time and your local timezone.

This code doesn't simply truncate- it discards timezone information. But for an event scheduler used across the world, tracking timezones is important. You can't just throw that information away.

[Advertisement] Plan Your .NET 9 Migration with Confidence
Your journey to .NET 9 is more than just one decision.Avoid migration migraines with the advice in this free guide. Download Free Guide Now!

Source link

Subscribe to our magazine

━ more like this

2026 customer service planning series: Vol. 04

Real-world example Anthropic ran a focused “Fin hackathon” sprint to improve their AI Agent’s resolution rate. The team audited unresolved queries, identified underperforming topics, and...

Anonymice – The Daily WTF

Three blind anonymice are unbothered by the gathering dark as we approach...

Dior, Chanel and … Veja? The ethical Paris trainer worn by A-listers and royalty | Fashion

In the grand hierarchy of Paris fashion, it’s tricky for a brand to stand out. Especially one whose coup de maître is a goes-with-everything...

The New ‘Paranormal Activity’ May Have Already Found Its Director

At the top of December, Paramount revealed it...