django 日期字段到 Unix 时间戳
在模型中我有一个这样的字段: mydate = models.DateField()
现在,javascript 图形函数需要 unix 时间戳,例如“1196550000000”,如何返回 mydate 输入的 unix 时间戳。
谢谢
In a model I have a such field:
mydate = models.DateField()
now a javascript graph function requires unix timestamp such as "1196550000000", how can I return the unix timestamp of my mydate input.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
编辑:请检查第二个答案,它有一个更好的解决方案
在 python 代码中,您可以执行此操作将日期或日期时间转换为 Unix Epoch
但这在 Django 模板中不起作用,所以你需要一个自定义过滤器,例如:
edit: please check the second answer, it has a much better solution
In python code, you can do this to convert a date or datetime to the Unix Epoch
This doesn't work in a Django template though, so you need a custom filter, e.g:
在
views.py
中,您可以将mydate
的值转换为自 Unix 纪元以来的秒数,如下所示:然后将其传递到用作
render_to_response()
(或任何您用来渲染视图的内容),然后在模板中,将{{seconds}}
粘贴到隐藏字段中,然后您可以将其拉出将 DOM 传递给您的 javascript 图形函数。请注意,
DateField
映射到 Python 对象datetime.date
,因此,其timetuple
将设置小时、分钟和秒字段到 0。如果这对您来说不够细粒度,您需要将mydate
更改为 DateTimeField,它将是datetime.datetime
。如果您这样做,您仍然可以使用mydate.timetuple()
。另外,我假设您使用的是当地时间。如果您使用 UTC 时间,则需要
calendar.gmtime()
而不是time.mktime()
和mydate.utctimetuple()
而不是比mydate.timetuple()
更好,但utctimetuple()
只是datetime.datetime
对象的有效方法。请参阅datetime
文档(还有time 和
calendar
) 了解更多细节。编辑:一些繁琐的细节,例如 mktime() 返回一个浮点数,piquadrat 记得而我没有。自定义过滤器方法也是一种不错的方法。投票赞成。
In your
views.py
, you can convert the value ofmydate
to seconds since the Unix epoch as follows:Then pass it in the dictionary you use as an argument to
render_to_response()
(or whatever you're using to render your view), and in your template, stick{{seconds}}
into a hidden field, which you can then pull out of the DOM to pass to your javascript graph function.Note that a
DateField
maps to the Python objectdatetime.date
, and as such, itstimetuple
will have its hours, minutes and seconds fields set to 0. If that's not fine-grained enough for you, you'll need to changemydate
to a DateTimeField and it'll be adatetime.datetime
. You can still usemydate.timetuple()
if you do this.Also, I'm assuming you're using local time. If you're using UTC time, you want
calendar.gmtime()
rather thantime.mktime()
andmydate.utctimetuple()
rather thanmydate.timetuple()
, bututctimetuple()
is only a valid method fordatetime.datetime
objects. See thedatetime
docs (alsotime
andcalendar
) for more fiddly details.EDIT: fiddly details such as the fact that
mktime()
returns a float, which piquadrat remembered and I didn't. The custom-filter approach is also a good one. Voting that one up.另一种选择:
Another option:
我在答案中没有找到一个非常简单的方法
A very simple way that I did not find in the answers
我知道不久前另一个答案已被接受,但这个问题在谷歌搜索结果中排名靠前,所以我将添加另一个答案。
如果您在模板级别工作,则可以使用
U
参数 到date
过滤器,例如:请注意,它将基于您的
TIMEZONE
设置.py。I know another answer was accepted a while ago, but this question appears high on Google's search results, so I will add another answer.
If you are working at the template level, you can use the
U
parameter to thedate
filter, e.g.:Note that it will be based upon the
TIMEZONE
in your settings.py.如果您不在模板层,您仍然可以使用相同的底层 django utils。前任:
And if you're not in the template layer, you can still use the same underlying django utils. Ex: