Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert date as UTC #424

Open
lpil opened this issue Apr 4, 2023 · 6 comments
Open

Insert date as UTC #424

lpil opened this issue Apr 4, 2023 · 6 comments

Comments

@lpil
Copy link

lpil commented Apr 4, 2023

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

@bombillazo
Copy link
Collaborator

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

@lpil
Copy link
Author

lpil commented Feb 12, 2024

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

@bombillazo
Copy link
Collaborator

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.

@lpil
Copy link
Author

lpil commented Feb 12, 2024

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.

@bombillazo
Copy link
Collaborator

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.

@lpil
Copy link
Author

lpil commented Feb 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants