My postgres database has users table and I am storing creation date in UTC (DB timezone is not set and is UTC). So if someone from NY signed up on 26 March at 11PM Local time, it will actually be saved as 27 March 3AM, so far so good. The issue I have is when I display that as stats, as in how many users were signed up today or in a specific date, things get hairy. Do I need to send the DB from the user's timezone and query the DB based on that?
-
1yep, "...signed up today..." requires you to decide about a time zone reference. Ask the user, he/she needs to decide what's the "reference time zone". And get that in written, just sayin'The Impaler– The Impaler2020-03-28 12:02:36 +00:00Commented Mar 28, 2020 at 12:02
-
Agreed. The concept of "today" depends on a time zone. In some cases you will want the user's time zone. In other cases it may be your business's time zone, UTC, or some other time zone. Once you decide, you can convert from start/end of day in that time zone to UTC, then use those values to query your db.Matt Johnson-Pint– Matt Johnson-Pint2020-03-28 20:03:35 +00:00Commented Mar 28, 2020 at 20:03
Add a comment
|
1 Answer
You can convert to the local timezone with:
SELECT timezone('America/New_York', created_at)::date
Here is a db<>fiddle, which at most times will give different dates.