Django在模板上使用javascript的日期 [英] Django date to javascript at the template

查看:53
本文介绍了Django在模板上使用javascript的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面实现这一目标的简单方法是什么?selected_date来自django上下文,作为python日期:

What would be the easy way of achieving such below? selected_date comes from django context as a python date :

<script type="text/javascript">
    var selected_year = {{ selected_date|date:"Y" }}
    var selected_month = {{ selected_date|date:"m" }} - 1;
    var selected_day = {{ selected_date|date:"d"}}
    var selected_date = new Date(selected_year, selected_month, selected_day);
    alert(selected_date);
</script>

推荐答案

我在python中使用isoformat函数取得了很多成功:

I've had a lot of success with the isoformat function in python:

var selected_date = new Date("{{ selected_date.isoformat }}")

这篇关于Django在模板上使用javascript的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆