Return datetimes in defined timezone
See original GitHub issueIssue 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:
- Created 5 years ago
- Comments:12 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:
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
: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:
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.
Can someone help me with #6059