Date Handling in Data Grids

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