Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将jinja代码中的时间值转换为浏览器';页面加载时的本地时区是多少?_Javascript_Python_Flask_Jinja2_Momentjs - Fatal编程技术网

Javascript 如何将jinja代码中的时间值转换为浏览器';页面加载时的本地时区是多少?

Javascript 如何将jinja代码中的时间值转换为浏览器';页面加载时的本地时区是多少?,javascript,python,flask,jinja2,momentjs,Javascript,Python,Flask,Jinja2,Momentjs,我有一个烧瓶生成的时间值列表,格式为:YYYY-DD-MM hh:MM:ss。这些时间值用作url链接,并使用jinja2显示在我的html文件中,如下所示: {% for job in jobs %} <a href=”{{ url_for(‘jobs/by_id’,job_id=job.job_id) }}"> {{ job.finishtime }} #this is the time value </a> {% e

我有一个烧瓶生成的时间值列表,格式为:YYYY-DD-MM hh:MM:ss。这些时间值用作url链接,并使用jinja2显示在我的html文件中,如下所示:

{% for job in jobs %} <a href=”{{ url_for(‘jobs/by_id’,job_id=job.job_id) }}"> {{ job.finishtime }} #this is the time value </a> {% endfor %} {作业%中作业的%s} {%endfor%} 我希望这些时间值自动转换为浏览器的本地时区。我尝试使用flask moment扩展,但在遵循所有说明并使用
{moment(job.finishtime).format('MMMM-Do-YYYY,h:mm:ss-a')}
之后,加载html页面时时间消失了。我也尝试了其他的力矩函数,但总是出错

有没有更好的方法让moment.js与我的jinja代码对话


我希望不必处理python datetime。

不幸的是,Jinja2是在服务器上呈现的,因此它的代码从未在客户机上运行过——它无法(轻松)获得时区。
这意味着Javascript在呈现Jinja2模板之后运行,因此不能在Jinja2标记中使用JS

有几种方法可以解决这个问题:

1) 当用户第一次访问您的站点(或登录,如果可能的话)时,让一些javascript设置一个
时区
cookie。然后可以在服务器端读取,并将其传递到模板中,如下所示:

# On the server
def view_jobs_page(request, response):
    import datetime
    tz = datetime.timedelta(seconds = int(request.cookies["timezone"])) # Pass this into the Jinja2 render function
    # ... Do stuff


# In your template
local time: {{ job.finishtime + tz }}
如果不知道所使用的确切堆栈和代码结构,就很难给出更详细的示例。这也有使用
datetime
的缺点,这是您不想要的

2) 使用javascript应用时间增量:

<!-- In the head -->
<script>
// When the page loads - the Jinja template is already rendered now
window.onload = function() {
  // Get all the elements with the given class, and map through them
  document.querySelectorAll(".datetime_to_be_adjusted").forEach(function(el){
    var utcTime = moment.utc(el.innerText);  // This is the time in UTC
    utcTime.local();  // Switch to using the browser's local timezone
    el.innerText = utcTime.format('MMMM Do YYYY, h:mm:ss a');  // Write the local time back to the element
  });
}
</script>
...
{% for job in jobs %}
    <a href=”{{ url_for(‘jobs/by_id’,job_id=job.job_id) }}" class="datetime_to_be_adjusted">
        {{ job.finishtime }}
    </a>
{% endfor %}

//当页面加载时,Jinja模板现在已经呈现
window.onload=函数(){
//获取给定类的所有元素,并映射它们
document.queryselectoral(“.datetime\u to\u be\u adjusted”).forEach(函数(el){
var utcTime=moment.utc(el.innerText);//这是utc中的时间
utcTime.local();//切换到使用浏览器的本地时区
el.innerText=utcTime.format('MMMM Do yyy,h:mm:ss a');//将本地时间写回元素
});
}
...
{作业%中作业的%s}
{%endfor%}

这当然依赖于页面中包含的momentJS,并使用它。

尝试使用Js部分时出错,必须更改为:
let dates=document.queryselectoral(“.datetime\u to\u be\u adjusted”);dates.forEach(函数(el){let utcTime=moment.utc(el.innerText);//这是utc utcTime.local()中的时间;//切换到使用浏览器的本地时区el.innerText=utcTime.format('MMMM-Do-yyy,h:mm:ss-a');//将本地时间写回元素})