Menu

Opening hours component in React with time zones
📰

Opening hours component in React with time zones

DEV Community: react·Marco Schulz·13 days ago
#KQfIHgxH
#devtoreact#devto#dev#open#hours
Reading 0:00
15s threshold

The first "Open now" badge I shipped for a client lied. The salon was in Zurich, the site was deployed on servers in the US, and I was testing from my laptop at home. It showed "Open" at 11 pm because somewhere in the chain a date was created in the wrong time zone. That bug is the reason the Opening hours component and the Open now badge in Facade compute everything in the business's own IANA time zone and never trust the raw clock. Here is how they work.

Why new Date() on the client is wrong

new Date() gives you the visitor's local time. A visitor in Los Angeles looking at a barber in New York gets a Date whose getHours() is three hours behind the shop. Compare that to the shop's hours and the badge is wrong for every out of town visitor, which on a site that ranks for the city name is most of them.

The server is no better. On Vercel or any cloud host the server clock is UTC or whatever region the function runs in.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.