12

Anyone knows how to persist UTC Timestamp in Firestore?

In my Angular app, if I convert today's date to a Timestamp like the following and I end up with a UTC+2 (it's summer time here right now in Switzerland) date in the Firestore database

import {firebase} from '@firebase/app';
import '@firebase/firestore';

const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());

If for example I try to convert a date from the winter time, I end up with UTC+1 Timestamp in the database

const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date(2019, 0, 1));

If I would use now() I end up with UTC+2 dates too

const now: firebase.firestore.Timestamp = firebase.firestore.Timestamp.now();

I don't do anything particular when I persist the data:

const now: firebase.firestore.Timestamp = firebase.firestore.Timestamp.now();
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());

const myData = {
    created_at: now,
    another_value: myTimestamp
};

await this.collection.add(myData);

Any idea how is it possible to create valid UTC Timestamp for Firestore?

1 Answer 1

23

Firestore timestamps don't have a timezone encoded into them. It just stores the offset from the Unix epoch using a combination of seconds and nanoseconds (which are fields on the Timestamp object). Most date/time objects are like this - they don't care what the timezome is, it's just an absolute point in time.

If you view a timestamp field in the console, you will see the time displayed in the local timezone that your computer uses from its local settings.

If you convert a timestamp to a JavaScript Date object, that date object naturally renders itself in the browser's local timezome, similar to the console.

If you want to render a Date object for a specific timezone, you should use a library to do that for you, such as moment.js.

8
  • As I described above, when I use fromDate for example, it calculates an offset to my date including the timezone I kind of have the feeling, strangely. When I browse the DB in the Cloud admin, I do see UTC+2 or UTC+1. Furthermore what's also strange is that when I query the db from a Firestore Functions (which run in an UTC+0 environment) I can't retrieve the data except if I would add, for testing purpose, 1 or 2 hours extra for the timezone difference But yes on the paper I'm agree with you, Timestamp should not consider timezone Apr 16, 2019 at 19:55
  • 1
    Firestore timestamps just do not have anything to do with timezones. I don't see any evidence that they do. It's only when they're rendered on screen that you see a timezone. Apr 16, 2019 at 21:42
  • 2
    It shouldn't matter which timezone the client is in. A point in time is the same no matter where you are in the world - everyone uses the same unix epoch integer offsets to represent that time. However, if you use a Date object to construct a point in time based on hours, minutes, and second, people all over the world will come up with different values, because each TZ has a different 12pm. Apr 17, 2019 at 5:25
  • 7
    Hey so I am having this issue too. I am seeing in the Firestore console that the date is UTC +2 which is the current TZ that I am in. So that means that firestore is just formatting the UTC date for me in my browser but it is storing it in UTC correctly? If so, I just lost 3 hours trying to make sure that date said UTC +0. Sometimes I hate firestore. Apr 26, 2019 at 10:51
  • 2
    I really found this confusing at first esp that it is a database and you expect that what you see is what you get. Regardless of firestore's timestamps have nothing to do with timezones, shouldn't firestore console just be showing the actual saved timestamp and not convert it to local time? At first glance, it's seems like a data integrity and accuracy issue until you spent hours debugging. It's a waste of time for something that should just be trivial. Sorry, I find this implementation really stupid. I am starting to hate some of the Google products lately -- firestore and Go to mention :(
    – duduwe
    Apr 7, 2021 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.