Best way saving and showing time depending timezones?

Issue

Here is my problem, I have a Chat app and I want to save and show the sending time of the messages locally.

Example: I live in London and I sent a message at 19 O’clock (London time) to a friend in Dubai(Dubai time 23 O’clock), so sending time should appear at 19 and receiving time at 23 O’clock on friend’s phone. And If I move to other time zone that should be considering.

Any tips please?

Solution

You should be saving the time in one format and that is the one on your server (for consistency)
Time is usually saved in UTC format (but technically you could have any time you want)

When the user receives the data with the time in UTC format, your client side app will transform it to the local format.

Example:(I’ll show the example with a time zone different than London since there is no difference between London and UTC)

User A lives in New York with current time 12: 01 AM (EDT).
User B lives in Dubai with current time 08: 01 AM (GMT+4).
The UTC time stored on the server will be 04:01 AM

When User A tries to send a message it will be recorded as 04:01 AM UTC on the server, which is received by User B who will convert 04:01 Am to 08: 01 AM.

So basically all you have to do is convert from the server time to the user’s local time (which you can get through whatever programming language you are using).

Answered By – Gabriel

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.