Reputation: 290
I'm uploading timestamps to Firestore documents from my flutter application as DateTime(2020, 02, 29, 9)
The timestamp gets stored in Firestore as February 29, 2020 at 9:00:00 AM UTC-6
Retrieving the timestamps from the flutter application using document['timestamp'].toDate().hour
returns '9' as expected.
The issue occurs when getting this data in a cloud function. snap.timestamp.toDate()
returns the date object as Sat Feb 29 2020 03:00:00 GMT+0000 (UTC)
not considering the timezone offset.
How can I retrieve the same time as stored in firestore instead of getting it in UTC.
Upvotes: 6
Views: 4148
Reputation: 317808
Timestamps in Firestore are represented as offsets from epoch time in UTC. There is no timezone encoded into a timestamp. When you view a timestamp field in the Firestore console, it will format the date using the configured timezone of the local computer.
JavaScript Date objects are similar - they also do not store timezones. When print a string representation of a Date, it will add a timezone, but that is just part of the string formatting. Don't use that string format if you want to deal with dates and timestamps without timezone.
If you want to deal with dates without timezones, you should be ignoring formatted string representations of those dates. Just deal with them programmatically, or use a date library that lets you format a date using the timezone you would prefer.
Upvotes: 8