question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return datetimes in defined timezone

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Is it possible to return all timestamps in different defined timezone? For example I save datetimes to postgres timestamp with timezone column, it is saved in UTC as expected. But I would like to return them in different timezone. I tried to set node app like process.env.TZ = "Europe/Prague" but with no success. This only change how datetimes are saved if time offset is not specified.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
chriskalmarcommented, May 5, 2018

@pleerock thanks for the nice intro 😎

As for your timezone problem @michalhorcic, I’d love to know why you would like to have timestamps rendered into a specific timezone on the server. UTC is your friend. Imagine having 2 clients - one sitting in New York and one sitting Paris - both receive the same UTC timestamp. Thanks to JavaScript in the browser converting that timestamp into their respective timezone aware local time is as simple as this:

new Date('2018-05-05T10:00:00.000Z')

So all the hard work of figuring out the offsets and doing pointless calculations is taken care of. Plus DST (Daylight saving time) is done automatically as well. (if a government decides one day to stop using DST you won’t have to handle this, because the browser will).

As our world is a complicated place there are timezones on our planet that don’t stick to the full-hour offsets, sometimes you will find 30 minute and even 45 minute offsets (https://en.wikipedia.org/wiki/Time_zone). Another important aspect is that not all browsers work with timezones correctly. So it’s a good idea to resort to dedicated libraries instead. I can recommend 2 of them:

Now having said that, there are ways to transform dates into a specific timezone on the server. Postgres has all the tools you need. Here is an example of how to convert a UTC time to the timezone +2:

postgres=# SELECT ((timestamp '2018-05-05 10:00:00+0') AT TIME ZONE '+2');
        timezone
------------------------
 2018-05-05 12:00:00+00

typeorm gives you all the freedom to write custom field selectors or even entirely custom queries.

Another option would be - as you already mentioned - a small middleware between typeorm and your data-fetching logic to automatically translate timestamps. With moment-timezone (yes, it works on the server as well) it would look something like this:

moment.tz('2018-05-05T10:00:00.000Z', 'Europe/Vienna').format() 
// result: "2018-05-05T12:00:00+02:00"

Finally, I’d like to stress the point that using UTC at data transport and converting to local times on the client is the preferred way in most cases.

0reactions
Kannas24commented, May 14, 2020

Can someone help me with #6059

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting times between time zones - Microsoft Learn
Assumes the dateTime parameter is local time and converts local time to UTC. DateTimeKind.Utc, Returns the dateTime parameter unchanged. The ...
Read more >
How do I get a value of datetime.today() in Python that is ...
Worst case, is there a way I can manually enter a timezone value into the datetime object returned by datetime.datetime.today() and set it...
Read more >
How to Handle Time Zones using DateTime and Luxon
The toLocaleString method returns a string with a language-sensitive representation of the Date object. At the same time, this method supports ...
Read more >
Working With TimeZones in Python - PYnative
Create datetime in a different timezone using pytz and timezone. Get timezone name, UTC offset, and DST offset. Timezone conversion.
Read more >
datetime — Basic date and time types — Python 3.11.1 ...
Date and time objects may be categorized as “aware” or “naive” depending on whether or not they include timezone information. ... A naive...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found