Django 日期到 javascript 模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5076319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:51:43  来源:igfitidea点击:

Django date to javascript at the template

javascriptdjangodjango-templates

提问by Hellnar

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

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

<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>

回答by Exelian

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

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

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

回答by rych

The accepted answer may generate an incorrect date depending on locale.

根据区域设置,接受的答案可能会生成不正确的日期。

In FF console:

在 FF 控制台中:

>>> n = new Date('2011-01-01');
Date {Fri Dec 31 2010 16:00:00 GMT-0800 (PST)}

It's therefore preferable to pass Y,m,d integers to the Date constructor.

因此最好将 Y,m,d 整数传递给 Date 构造函数。

I use a template filter to generate the date constructor:

我使用模板过滤器来生成日期构造函数:

@register.filter(name='jsdate')
def jsdate(d):
    """formats a python date into a js Date() constructor.
    """
    try:
        return "new Date({0},{1},{2})".format(d.year, d.month - 1, d.day)
    except AttributeError:
        return 'undefined'

回答by Pamela Fox

I was using this and realized Android was returning 'Invalid Date' when parsing it - I think it's stricter than the desktop Webkit. I am instead using the following, which seems to work:

我正在使用它并意识到Android在解析它时返回“无效日期” - 我认为它比桌面Webkit更严格。我改为使用以下内容,这似乎有效:

new Date('{{ talk.start_datetime|date:"D, d M Y H:i:s"}}'),

More on JS date parsing is here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse

更多关于 JS 日期解析的信息在这里:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse

回答by Agent Francis

If you are using your date in JS, you will want to use the escapejs filter.

如果您在 JS 中使用日期,则需要使用 escapejs 过滤器。

<script type="text/javascript">
    var selected_date = new Date({{ selected_date|escapejs }});
    alert(selected_date);
</script>

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#escapejs

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#escapejs

回答by Sven Rojek

Since other approaches are not timezone aware, you can simply use the Django's built-in Template Tag widthratioto convert python's seconds to javascript's milliseconds.

由于其他方法不知道时区,您可以简单地使用Django 的内置模板标签宽度比率将 python 的秒数转换为 javascript 的毫秒数。

new Date({% widthratio selected_date|date:"U" 1 1000 %}