Home > Mobile >  How to handle BST and GMT timezones on client side? Storing them in a DB as UTC
How to handle BST and GMT timezones on client side? Storing them in a DB as UTC

Time:09-28

I've got a Go app and I store all datetimes as UTC:

dateTime, err := time.Parse("2006-01-02T15:04:05.000Z", myDateTime)

This becomes something like: 2022-09-29T19:40:36.150Z.

Now I want to show this datetime client side (on a website), but showing the user the time in their timezone.

Since it's all UTC I could have the user pick their timezone from a list, like this one https://gist.github.com/valo/c07f8db33d223f57a4cc9c670e1b6050.

Then it's just a matter of adding/subtracting some time from the UTC and showing it.

But the problem is with daylight savings time, the client side timezone would have to change when it's summer, and change again when it's winter.

How are people doing this?

CodePudding user response:

Usually, this happens on the client side. Information about the client's timezone is stored on the browser and is available to you if you need it. However, there are functions that sort this out automatically like toLocaleDateString.

Step 1. Parse the time string 2022-09-29T19:40:36.150Z to a Date object in javascript

Step 2. Use toLocaleDateString on the date. This will display the Date in the client's timezone setting stored in the browser

MDN docs for toLocaleDateString()

  • Related