I’m seeing what seems to be an inconsistency in date handling. I have a form where I have a couple spots that display dates.
One is a date/time picker that I set using a date retrieved from a data table field.
The other is a column in a data grid that displays a date from a data table field.
Both are populated by data returned from calls to server functions.
What I’m seeing is that the date/time picker is adjusting the time for my local time zone, but the data grid column is displaying exactly what’s in the data table.
I was expecting the data grid column to adjust for time zone, too. I set it using the result from an app_tables query, like normal.
Just run it, and a form with a date picker and a data grid will show up. The date picker is showing the date whose date id is 1. In the picker it shows as adjusted for the time zone, in the data grid it does not.
Is this an inconsistency, or am I missing something?
That’s correct, DatePickers display the time in your local timezone. If you’re picking a time at your local machine, you naturally expect to use your timezone rather than UTC (or anything else). The original datetime object they’re given retains the timezone it started with.
By default, Data Grids display the str() of whatever they’re given, so you get the datetime in the timezone its tzinfo is set to. In this case, since they’re coming from the server, they’re in UTC. (In Anvil, we avoid incorrect time comparisons by stamping datetime objects with the timezone they were created in as soon as they’re sent over the network - see this Forum post for more info.)
You can modify your Data Grid to use your browser’s timezone by dropping a Label into the relevant column of your Data Grid and running this line in the RowTemplate’s __init__ method:
While this may functioning the way it’s designed, it seems inconsistent at best. If one UI widget adjusts for the local timezone automatically, why don’t all of the type aware widgets? The data grid knows that it’s dealing with a datetime field, right?
I can use the workaround you posted for now, but it would be far more consistent for the data grid to automatically display dates in the local time zone.