Member-only story
In an online auction project, the users were spread in a variety of time zones. Each had to see the accurate time for the end of the auction, while some users would create auctions from the web page. This required us to juggle timezones at the front-end and the backend.
Dates, Times, and Time Zones
A user of a web application has to see the dates and times in his/her local time zone. These dates and times in a SPA, or dynamically rendered parts of a we page, are obtained by JavaScript from the browser, using new Date(dateString)
This dateString
comes from the server, so the question is how is it stored.
An Express Backend
Using an Express backend, we stored data in MongoDB. MongoDB store data in UTC which is an absolute unique time, no matter where MongoDB is placed and where the server is located.
Getting the Date and Time from the Web Page
The dates and times to be stored reach the server from a web form, where the user filling it can be anywhere in the world. We are just asking the user for a date and a time, so we get the time zone using JavaScript on the page with:
const timeZone =
Intl.DateTimeFormat().resolvedOptions().timeZone;