我想更改时间计算格式(django模型,虚拟字段)
I want to change the time calculation format (django models, virtual field)
模型字段代码是这个
created = models.DateTimeField(auto_now=True)
@property
def now_diff(self):
return timezone.now() - self.created
我有一个关于使用虚拟字段的django时间循环的问题
当前时间循环输出为5:26:34.349728
但是我想5:26:34
有办法吗?
谢谢你告诉我~!
我改成了
@property
def now_diff(self):
s=timezone.now() - self.created
hours, remainder = divmod(s, 3600)
minutes, seconds = divmod(remainder, 60)
return '{:02}:{:02}:{:02}'.format(int(hours), int(minutes), int(seconds))
但是错误
TypeError: 'Todo' object is not subscriptable
TypeError: unsupported operand type(s) for divmod(): 'datetime.timedelta' and 'int'
也许你可以这样尝试(主要是从this答案中复制粘贴):
@property
def now_diff(self):
time_delta = timezone.now() - self.created
s = time_delta.seconds
hours, remainder = divmod(s, 3600)
minutes, seconds = divmod(remainder, 60)
return '{:02}:{:02}:{:02}'.format(int(hours), int(minutes), int(seconds))
从timedelta
中减去微秒
from datetime import timedelta
class MyModel(models.Model):
...
created = models.DateTimeField(auto_now=True)
@property
def now_diff(self):
<b>delta = timezone.now() - self.created
return str(delta - timedelta(microseconds=delta.microseconds))</b>
为了更易读的解决方案,
from datetime import timedelta
<b>def chop_microseconds(delta):
return delta - timedelta(microseconds=delta.microseconds)</b>
class MyModel(models.Model):
...
created = models.DateTimeField(auto_now=True)
@property
def now_diff(self):
return <b>str(chop_microseconds(timezone.now() - self.created))</b>
一些BG东西
如果我们查看 timedelta
的 Source code of __str__()
函数 class,
....
if self._microseconds:
s = s + ".%06d" % self._microseconds
...
转换 timedelta 对象的字符串表示形式。
所以,这里我们从 timedelta 中减去 microsecond 从而解决了问题:)
模型字段代码是这个
created = models.DateTimeField(auto_now=True)
@property
def now_diff(self):
return timezone.now() - self.created
我有一个关于使用虚拟字段的django时间循环的问题
当前时间循环输出为5:26:34.349728
但是我想5:26:34
有办法吗?
谢谢你告诉我~!
我改成了
@property
def now_diff(self):
s=timezone.now() - self.created
hours, remainder = divmod(s, 3600)
minutes, seconds = divmod(remainder, 60)
return '{:02}:{:02}:{:02}'.format(int(hours), int(minutes), int(seconds))
但是错误
TypeError: 'Todo' object is not subscriptable
TypeError: unsupported operand type(s) for divmod(): 'datetime.timedelta' and 'int'
也许你可以这样尝试(主要是从this答案中复制粘贴):
@property
def now_diff(self):
time_delta = timezone.now() - self.created
s = time_delta.seconds
hours, remainder = divmod(s, 3600)
minutes, seconds = divmod(remainder, 60)
return '{:02}:{:02}:{:02}'.format(int(hours), int(minutes), int(seconds))
从timedelta
中减去微秒
from datetime import timedelta
class MyModel(models.Model):
...
created = models.DateTimeField(auto_now=True)
@property
def now_diff(self):
<b>delta = timezone.now() - self.created
return str(delta - timedelta(microseconds=delta.microseconds))</b>
为了更易读的解决方案,
from datetime import timedelta
<b>def chop_microseconds(delta):
return delta - timedelta(microseconds=delta.microseconds)</b>
class MyModel(models.Model):
...
created = models.DateTimeField(auto_now=True)
@property
def now_diff(self):
return <b>str(chop_microseconds(timezone.now() - self.created))</b>
一些BG东西
如果我们查看 timedelta
的 Source code of __str__()
函数 class,
....
if self._microseconds:
s = s + ".%06d" % self._microseconds
...
转换 timedelta 对象的字符串表示形式。
所以,这里我们从 timedelta 中减去 microsecond 从而解决了问题:)