0

I have an aware datetime-object and want to return an aware datetime-string. However when I do

obj.strftime("%e %b %Y %H:%M:%S")

it returns a UTC-based string (even though TIME_ZONE-setting is set right).

How can I get a localised datetime-string?

EDIT: I want the timezone of django to get applied

F.M.F.
  • 1,929
  • 3
  • 23
  • 42
  • 2
    Possible duplicate of [Python datetime strptime() and strftime(): how to preserve the timezone information](https://stackoverflow.com/questions/14762518/python-datetime-strptime-and-strftime-how-to-preserve-the-timezone-informat) – TheoretiCAL Sep 05 '17 at 21:53
  • 1
    https://github.com/django/django/blob/master/django/utils/dateformat.py i think OP expect a solution using the django defined timezone – PRMoureu Sep 05 '17 at 21:58
  • yes thats right – F.M.F. Sep 05 '17 at 22:01

2 Answers2

1

You can use the module dateformat inside your project, it allows to get the timezone from your settings :

from django.utils import dateformat

timezoned_date = dateformat.DateFormat(obj).format('jS F Y H:i:s')

EDIT :

This works fine with unaware datetime objects, better use django.utils.timezone.localtime with aware dates.

PRMoureu
  • 12,817
  • 6
  • 38
  • 48
1

I had to use localtime(obj):

localtime(obj).strftime("%e %b %Y %H:%M:%S")

worked fine.

F.M.F.
  • 1,929
  • 3
  • 23
  • 42