1

How can I change a timezone in a datetimefield.

right now I have

datetime.datetime(2013, 7, 16, 4, 30, tzinfo=<UTC>)

how can modify the tzinfo just for display not to update on the db.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
yaniv14
  • 691
  • 1
  • 10
  • 24
  • Are you talking about printing it as a string? In which case, would this help? http://stackoverflow.com/a/311655/2620328 – sihrc Aug 04 '13 at 13:29

1 Answers1

1

Use pytz for such things.

From the pytz docs, you can use astimezone() to transform time into different time zone, as example below.

>>> eastern = timezone('US/Eastern')
>>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
>>> loc_dt = utc_dt.astimezone(eastern)
>>> loc_dt.strftime(fmt)
'2002-10-27 01:00:00 EST-0500'
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • don't use `tzinfo` parameter for timezone with non-fixed utc offset e.g., it is ok to use it with `utc` (the utc offset is zero (always)) but it is wrong to use it with `eastern`. Use `aware_dt = eastern.localize(naive_dt, is_dst=None)` instead. – jfs Mar 24 '15 at 07:58