将格林尼治标准时间转换为Django当地时间

时间:2019-09-10 07:13:51

标签: django utc localtime

如何将UTC时间转换为本地时间?

以下是前端的格式:Tue Sep 10 2019 00:00:00 GMT+0800 (Singapore Standard Time)

这是Django的打印结果:2019-09-09T16:00:00.000Z

这就是我转换为当地时间的方式:

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)

这是函数的结果:2019-09-09

我的预期结果:2019-09-10

1 个答案:

答案 0 :(得分:0)

转到settings.py,您将看到类似这样的内容

TIME_ZONE = 'Asia/Kathmandu'

将其更改为新加坡。时区列表在此处列出:https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

有关更多信息,请访问:https://docs.djangoproject.com/en/2.2/topics/i18n/timezones/