Arkar
Arkar

Reputation: 53

Convert UTC time to local time in django

How can I convert to from UTC time to local time?

Here is the format from frontend: Tue Sep 10 2019 00:00:00 GMT+0800 (Singapore Standard Time)

This is the print result from Django: 2019-09-09T16:00:00.000Z

This is how I convert to local time :

def convert_to_localtime(utctime):

    # print(utctime)

    fmt = '%Y-%m-%d'
    utc = datetime.datetime.strptime(utctime,'%Y-%m-%dT%H:%M:%S.000Z' ).replace(tzinfo=pytz.UTC)
    localtz = utc.astimezone(timezone.get_current_timezone())

    # print(localtz.strftime(fmt))
    return localtz.strftime(fmt)

this is the result from function: 2019-09-09

My expected result: 2019-09-10

Upvotes: 1

Views: 972

Answers (1)

Kshitish
Kshitish

Reputation: 60

Go to settings.py, you will see something like this

TIME_ZONE = 'Asia/Kathmandu'

Change it to Singapore. The list of Timezones are listed here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

For further information goto: https://docs.djangoproject.com/en/2.2/topics/i18n/timezones/

Upvotes: 1

Related Questions