简体   繁体   English

django中的日期时间显示和时区转换

[英]datetime display and timezone converting in django

in settings.py file: 在settings.py文件中:

TIME_ZONE = 'UTC' 
USE_TZ = True

in models.py: 在models.py中:

from django.utils import timezone

date = models.DateTimeField(default = timezone.now())

before pip install pytz,date field in mysql db stored as UTC time, and date in front end webpage display as localtime. 在pip install pytz之前,mysql db中的date字段存储为UTC时间,而前端网页中的日期显示为localtime。 but after pip install pytz,date field in mysql db stored as UTC time, at the same time date in front end webpage display as UTC time too. 但是在pip安装pytz之后,mysql db中的date字段存储为UTC时间,同时前端网页中的日期也显​​示为UTC时间。

What is the reason for this? 这是什么原因呢? How to do make sure that after pip install pytz,date field in mysql db stored as UTC time, and date in front end webpage display as localtime? 在pip安装pytz之后,如何确保mysql db中的date字段存储为UTC时间,而前端网页中的日期显示为localtime? BTW, which library is more easily done than pytz? 顺便说一句,哪个库比pytz更容易完成?

Just change your TIME_ZONE setting to your local time's pytz . 只需将TIME_ZONE设置更改为您当地时间的pytz

eg 例如

TIME_ZONE = 'Asia/Dubai'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM