Website logo

22 Feb 2023

Why
You
Should
Save
Timestamp
in
UTC
Format

In an app, sometimes there is a feature that used to save a data with the timing (sometimes called as DateTime or Timestamp technically), either the time when the data saved or the time for other uses, i.e. the time when data will be publish. For example, a feature that used to create an article for a blog that later the article will be publish to the readers.

When the author used that feature, the author fill the form to create an article and set the publish time for that article and then he click the "Save" button to save that article in the database.

Create Article Feature

Let say the author is Indonesian and he create the article with publish time in Indonesia timezone (GMT+7), i.e. 21 Feb 2023 10:00. Then that article will be shown to the readers.

Because of the article was saved in the Indonesia timezone (GMT+7), the readers from Indonesia will not have a problem when reading the publish time of that article.

But what if the readers from another country that possibly have different timezone reads the publish time of that article not match with their timezone? For example, they read the article list but that article was not found published in 21 Feb 2023 10:00 in their timezone and of course this will make them confuse.

Solution

The solution is all the times that related with that data need to save in database in the UTC format. Then when that times need to be appears on the app, the times need to be formatted in the timezone where the readers access the app.

In that case, when the author create an article, even though the article's publish time set in the Indonesia timezone (GMT+7) from the app user interface, but when the article's publish time will be save to the database need to be formatted to the UTC format before. So from 21 Feb 2023 10:00 (GMT+7) become 21 Feb 2023 03:00 (UTC).

After that when publish time need to appears on the app user interface, it needs to be formatted from UTC to each of the timezone of the app users.

For example, the user from Indonesia using the app, so the article's publish time need to be formatted from UTC to GMT+7. Then there is a user from Japan using the app, so the article's publish time need to be formatted from UTC to GMT+9. We can format the time in the frontend.

Timestamp Formatting Flow

Conclusion

With the formatting process from user timezone to the UTC and vice-versa, this can prevent the user from many different timezone to confuse about the time of the data that appears on the app user interface. And it's better if you implement this when you start developing an app.