1

I have a React/Redux application.

I am fetching a set of "notes" from a Firebase Realtime Database. The "notes" has a Unix epoch timestamp "timestamp" stored in UTC time zone. I want the user to see all times in local time zone. Upon fetch I need to adjust the "timestamp" to reflect the users time zone.

I am creating db subscriptions to update notes in the store.

What would the best approach to adjust the UTC timezone to the users local time? I guess I first need to fetch the users settings and then adjust the "timestamp" on the fly?

My notes object

{
  title: "now",
  timestamp: "161651792000"
}

My user object

{
  displayName: "Mickey",
  userTz: "Europe\Stockholm",
  "userGmtOffset":3600"
}

Db subscription code

database.ref(`${dbPathAccess}/${uid}`).on('value', (myNoteAccessSnaps) => {
  ...
}

Many thanks! /K

Kermit
  • 2,865
  • 3
  • 30
  • 53
  • 1
    Is your question [How to convert a UTC timestamp to the user's local timezone](https://stackoverflow.com/search?q=%5Bjavascript%5D+How+to+convert+a+UTC+timestamp+to+the+user%27s+local+timezone)? – Frank van Puffelen Mar 23 '21 at 17:09
  • Hi Frank! Thank you for the prompt reply! :D No, my question was rather along the lines of "How can I convert all timestamps in the store upon load/update, given a known offset" The links were useful though, even if they did not answer my question (^__^) /K – Kermit Mar 23 '21 at 19:08

1 Answers1

1

Timezones only play a roll in how the date is displayed to a user. If a date object is constructed using a timestamp created by Firebase.ServerValue.TIMESTAMP, it will automatically have the same timezone as the local time zone. Just in case: firebase. firestore. Timestamp.toDate()

On the other hand getTimezoneOffset() can be used to get the client's time zone. Check documentation and help here in case you want to make any research.

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • Hi rustyBucketBay! I am using Cloud functions and trigger .onCreate(). The timestamp os fetched by context.timestamp and converted to date object using new Date(context.timestamp). I cannot figure out how to get the UTC Unix Epoch time though. /K – Kermit Mar 23 '21 at 20:17
  • So I am understanding that you are doing the new Date() from the timestamp in the server. That is the problem, right? – rustyBucketBay Mar 23 '21 at 20:27
  • I don't think I understand your question. A timestamp is an absolute point in time. .If you read a timestamp creating a new date, it will be in the client's local time. I guess that somewhere in your app you will read that timestamp from the database to display the date with a `toDate()` in the client, so the client will see that date according to his time zone from the absolute point in time the timestamp represents. Just as you demand in your question: `I want the user to see all times in local time zone. Upon fetch I need to adjust the "timestamp" to reflect the users time zone` – rustyBucketBay Mar 23 '21 at 20:40
  • Yeah, sorry. I got a bit confused. I did not realise that Unix Epoch is ALWAYS in UTC. X-D Also, as you pointed out - creating a new date using a timestamp will reflect the users time zone. Understanding those concepts solved my issue!! Many thanks! /K – Kermit Mar 30 '21 at 07:32