Date Handling in Data Grids

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.

You can see this behavior in this test app:

https://anvil.works/build#clone:L3FIB27QRVVXXDQ2=IDHWCBZM3N3KCRXFOAIRTDV3

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?

Hi @jshaffstall

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.

If you modify your app to do this:

    d1 = anvil.server.call('get_one_date')['date']
    print(d1)
    self.date_picker_1.date = d1
    print(self.date_picker_1.date)
    print(d1)

You get (if you’re in GMT+1 as I am):

2019-05-27 00:00:00+00:00
2019-05-27 01:00:00+01:00
2019-05-27 00:00:00+00:00

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:

import anvil.tz
#...
  def __init__(self, **properties):
    # ...
    self.label_1.text = str(self.item['date'].astimezone(anvil.tz.tzlocal()))

Here’s a modified version of your app that uses the browser’s local timezone to display the dates in the Data Grid:

https://anvil.works/build#clone:Z6XKV6CZVN3XUQGO=55W27KMACCI6G4RRTCBRB5AS

1 Like

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.