postgres
postgres copied to clipboard
Insert date as UTC
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
Hey could you share more info on how you're inserting? The value , query, etc?
I don't know any way to do this, I have nothing to share.
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.
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.
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.
That's right, aye. I was unable to find a way to insert a date with a specific timezone with this library.