Skip to content

Instantly share code, notes, and snippets.

@ferrouswheel
Last active December 14, 2015 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferrouswheel/5104085 to your computer and use it in GitHub Desktop.
Save ferrouswheel/5104085 to your computer and use it in GitHub Desktop.
If you are using Django with USE_TZ=True, then Django stores times in UTC and various template utilities help to convert to the user's/current timezone as necessary. However, when serialising objects to JSON (which many API libraries do) they'll often directly convert the UTC time. This might be what you want, but you might also be using the API…
# assuming utc_datetime is the datetime to convert:
from django.utils import timezone
current_tz = timezone.get_current_timezone()
local_time = current_tz.normalize(utc_datetime).astimezone(current_tz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment