postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Insert date as UTC

Open lpil opened this issue 2 years ago • 6 comments

Hello!

I've some Date objects that I wish to insert into a timestamp without time zone column, but when I insert them they get recorded using my locale timezone. How can I insert them using UTC instead?

Thanks, Louis

lpil avatar Apr 04 '23 11:04 lpil

Hey could you share more info on how you're inserting? The value , query, etc?

bombillazo avatar Feb 12 '24 04:02 bombillazo

I don't know any way to do this, I have nothing to share.

lpil avatar Feb 12 '24 08:02 lpil

The query statement and code you're using with the values that are not showing up as UTC in your database is a good start.

bombillazo avatar Feb 12 '24 12:02 bombillazo

INSERT INTO test_table (date_column)
VALUES ($1);

Where the parameter is a JavaScript Date object.

The appropriate time zone to use is unknown as the Date class does not support timezones.

lpil avatar Feb 12 '24 17:02 lpil

Hmmm, I think the issue is that the JS date object stores dates in UTC, but with the offset of your client app's local timezone in the value, while your DB server is using another timezone (probably true UTC +0). I've seen this before and could only make it bearable to work with using a library like Dayjs, Moment, or Luxon that creates timezone-aware dates that take into account your local timezone when creating actual UTC dates.

bombillazo avatar Feb 12 '24 20:02 bombillazo

That's right, aye. I was unable to find a way to insert a date with a specific timezone with this library.

lpil avatar Feb 12 '24 20:02 lpil