How to add Indian Standard Time (IST) in Django?

前端 未结 13 556
终归单人心
终归单人心 2021-02-02 06:22

We want to get the current time in India in our Django project. In settings.py, UTC is there by default. How do we change it to IST?

相关标签:
13条回答
  • 2021-02-02 07:08

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'Asia/Calcutta'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True

    This should work.

    0 讨论(0)
  • 2021-02-02 07:08

    Change your settings.py to:

    TIME_ZONE =  'Asia/Calcutta'
    
    USE_I18N = True
    
    USE_L10N = True
    
    USE_TZ = False
    

    This should work.

    For more information about the TIME_ZONE in Django you can see: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone

    0 讨论(0)
  • 2021-02-02 07:09

    Change the field TIME_ZONE in the settings.py. For the Indian standard time you will need:

    TIME_ZONE =  'Asia/Kolkata'
    

    For more information about the TIME_ZONE in Django you can see: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone

    0 讨论(0)
  • 2021-02-02 07:13

    Use Below settings its worked for me. TIME_ZONE = 'Asia/Kolkata'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = False

    0 讨论(0)
  • 2021-02-02 07:14

    In general, Universal Time(UTC) based on the mean sidereal time as measured in Greenwich, England. It's also approximately equal to mean solar time from Greenwich. If Daylight Saving Time is in effect in the time zone, one must add 1 hour to the above standard times.

    Django got all these timezone support, The django.utils timezone module, just returns datetime based on the USE TZ setting, they are simply datetime objects with timezone 'awareness'.

    For IST(India Standard Time), which is UTC + 5:30, set TIME_ZONE = 'Asia/Kolkata' and USE_TZ = True, also USE_I18N = True, USE_L10N = True, which are Internationalization and localization settings.

    For a reference,

    from django.utils import timezone
    import datetime
    
    print(timezone.now())  # The UTC time
    print(timezone.localtime())  # timezone specified time,if timezone is UTC, it is same as above 
    print(datetime.datetime.now())  # default localmachine time
    
    # output
    2020-12-11 09:13:32.430605+00:00
    2020-12-11 14:43:32.430605+05:30  # IST is UTC+5:30
    2020-12-11 14:43:32.510659
    

    refer timezone settings in django docs for more details.

    0 讨论(0)
  • 2021-02-02 07:15

    All the solutions given above are working.

    see the code i have used to get my timezone. my timezone is Indian standard time zone.

    https://docs.djangoproject.com/en/3.0/topics/i18n/

    LANGUAGE_CODE = 'en-us'
    
    TIME_ZONE =  'Asia/Kolkata'
    
    USE_I18N = True
    
    USE_L10N = True
    
    USE_TZ = True
    
    0 讨论(0)
提交回复
热议问题