2

here is my timezone settings in settings.py

TIME_ZONE =  'Asia/Kolkata'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = 'en-us'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = False

now when I use timezone.now(), I am getting UTC time always. Did I miss something

venkateswar
  • 145
  • 1
  • 8

1 Answers1

4

First of all, you have to set USE_TZ = True to use the functionality.

Django converts the time to the specified timezone (Asia/Kolkata) only in templates. So, if you want to use your local time (Asia/Kolkata) somewhere in code (views/models) you have to use localtime() method. Follow the code to achieve it:

settings.py

TIME_ZONE = 'Asia/Kolkata'
USE_TZ = True

and,

from django.utils import timezone

my_local_time = timezone.localtime(timezone.now())

Example in django shell

In [1]: from django.utils import timezone

In [2]: timezone.now()
Out[2]: datetime.datetime(2018, 4, 29, 14, 5, 30, 112218, tzinfo=<UTC>)

In [3]: timezone.localtime(timezone.now())
Out[3]: datetime.datetime(2018, 4, 29, 19, 35, 46, 649587, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD>)

References:

  1. SO post
  2. localtime()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JPG
  • 82,442
  • 19
  • 127
  • 206