0

My api generates dates and saves them to a database which then will be read by the client.

Right now when I generate a date object (doing new Date() ) in node I get this:

2022-10-27T19:30:00.400Z

This is, as I understand, UTC+00, because when I insert that date in my postgres database (which have UTC set as its timezone) I get this (column type is timestampz):

2022-10-27 19:30:00.400+00

Now because the date object is being generated on the server side (which doesn't have the same timezone as the client), the client also sends its timezone offset in the get request.

My question is how can I modify the timezone of a date object? so instead of the last date I can obtain something like this:

2022-10-27 19:30:00.400-05

when inserting it to the database.

GhostOrder
  • 586
  • 7
  • 21
  • There is no timezone property in a Date Object. If you want to store the Date in the timezone the client is, one solution would be to add/subtract hours accordingly using **setTime()**. I don't see the point though, I would store all Dates in the server as UTC and then convert to proper timezone at the client. – Thanos Sakis Oct 27 '22 at 20:38
  • @ThanosSakis - Adding/subtracting hours will result in a different point in time, not a time zone adjustment. That approach is called "epoch shifting", and it is an antipattern in most cases (unless you are *very* careful about what you do with the resulting object). – Matt Johnson-Pint Oct 28 '22 at 21:32

0 Answers0