Change time zone (UTC) of Firebase

Viewed 10078

I have a webApp and not a app, when I send a function to Cloud Functions for Firebase new Data() don't get the my local time, used .toLocaleString('pt-BR', { timeZone: 'America/Sao_Paulo' }) and other ways of internet but no success.

I can't change timezone of my app in settings Firebase, because it's just a WebApp and not an app.

2 Answers

Is it really necessary to get the date/time as the local one?

If possible for your app, it's better to use UTC time on the server (your firebase function in this case) and transform it to local time on the client.

This way your firebase function won't have to deal with multiple timezones, daylight time saving, etc and will be simpler.

Sometimes it is necessary to process data based on client's timezone, even in backend.

E.g. if you have some stats arranged in the order of days, it will be wrong to use UTC because events are supposed to be in ONE day will end up in TWO days.

One thing I can think of, is to save the timezone's hour shift number (e.g. +12, +11, -4...) in a hard-coded manner, or save it to client's profile. Whenever a stat comes up and needs to be saved, find the hour shift and add it to the hour of the UTC DateTime.

Related