在一个模型中,我有这样一个字段:
现在javascript图形函数需要unix时间戳,比如"1196550000000",我如何返回mydate输入的unix时间戳呢?
谢谢
在一个模型中,我有这样一个字段:
现在javascript图形函数需要unix时间戳,比如"1196550000000",我如何返回mydate输入的unix时间戳呢?
谢谢
edit: please check the second answer, it has a much better solution个
在python代码中,可以执行此操作将日期或日期时间转换为unix纪元.
import time
epoch = int(time.mktime(mydate.timetuple())*1000)
但这在Django模板中不起作用,因此需要一个自定义过滤器,例如:
import time
from django import template
register = template.Library()
@register.filter
def epoch(value):
try:
return int(time.mktime(value.timetuple())*1000)
except AttributeError:
return ''