Firestore Store Date/Timestamp as UTC

Sep 26, 2019

I wanted to categorised my photos by day, thus each photo is given a day timestamp (in UTC) they belong to based on their local date.

  • 21 February 2019 at 18:18:13 UTC+8 -> 2019-02-21
  • 7 April 2019 at 11:34:32 UTC+12 -> 2019-04-07

Save to Database

val date = ... // LocalDateTime// remove time from dateval dayDate = date.truncatedTo(ChronoUnit.DAYS)// convert to UTC Timestamp for storageval timestamp = dayDate.toTimestamp(ZoneOffset.UTC)

NOTE: Refer to Covert LocalDateTime (java.time) to Firestore Timestamp

Read from Database

val timestamp = ...val dayDate = timestamp.toLocalDateTime(ZoneOffset.UTC)

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.