tommythecat
tommythecat

Reputation: 23

Firebase Timestamp returns with wrong date - Javascript

I'm trying to get the Timestamp value from firestore (using Firebase Functions), and I´ve successfully done it localy with the toDate() method of Timestamp, and moment library.

moment(doc.data().EndDate.toDate())

But when I deploy my code to firebase and test the function, somehow the toDate() returns a Date with 1 less hour than the saved timestamp on firebase. I suppose it is transforming my date to UTC, since I'm in UTC+1, and the Timestamp is also stored with UTC+1 in firestore, but I don't know how to reliably get the timestamp date as is in firestore, regardless of timezones.

If someone knows why this happens or has any idea how to solve it it would be great.

Upvotes: 2

Views: 2886

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

All timestamps in Firestore are stored in UTC. If you see something different in the Firebase console, that's just your browser formatting it for your local timezone.

In JavaScript, all Date objects are also represented in UTC. If you format that as a string, you will again possibly get a different representation based on your local timezone.

If you write code that computes values using dates or timestamps, you should perform all your computations using UTC. This is the pretty much all computing systems want to deal with dates. When it comes time to format the date for display to an end user, only then should you take timezone into account, and present something according to the user's preferences.

Upvotes: 4

Related Questions