[英]Convert UTC time from Datetimefield (models.py) to local time
A Event class in models.py models.py中的Event类
class Event(models.Model):
timestamp = models.DateTimeField()
message = models.TextField()
def __unicode__(self):
return "'%s' at %s" % (self.message, self.timestamp)
def api_detail(self):
return {
'timestamp': str(self.timestamp),
'description': self.message,
There is UTC time saved in database. 数据库中保存了UTC时间。 but i want to fetch it in localize time. 但我想在本地化时间获取它。 For example timestamp will return : Feb. 14, 2012 , 7 pm . 例如,时间戳将返回: 2012年2月14日,晚上7点 。 This time is in UTC i want to change it into a local time. 这次是UTC时间,我想将其更改为本地时间。
Please help me in this matter :) 请在这件事上帮助我:)
Local time in which time zone? 当地时间在哪个时区? The pytz documentation suggests that once you've decided which zone to use, it's as simple as: pytz文档建议,一旦您决定使用哪个区域,就很简单:
local_time = zone.localize(timestamp)
Note that converting from UTC to local time is unambiguous, whereas the reverse is not. 请注意,从UTC到本地时间的转换是明确的,而相反不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.