SQLAlchemy日期时间对象能否在Flas中显示本地时区

2024-09-30 20:32:25 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一篇Stackoverflow帖子,请原谅我的语法。
我试图在一个SQLAlchemy数据库中的date timestamp中使用不带时区的时间戳在用户的浏览器中显示他们所在时区的时间,而不是UTC。在

这是我的python/Flask代码(我是初学者):

首先我查询数据库

    timeclockinfo = TimeClock.query.filter_by(parent_id=current_user.parent_id, user_id=current_user.user_id, closed=1).all()

    #Then I tuple the data
    child_patient = zip(timeclockinfo, user_names, visit_dates )

    #I render the data with Flask
    return render_template('locationcheckinrpt.html', form=form, child_patient=child_patient, timeclockinfo=timeclockinfo, searchForm=searchForm)
    .
    .
    .
    #In the template I have a date time field for the records rendered
    {% for times in child_time %}
          {{  times[0].checkintime.strftime('%Y/%m/%d @ %I:%M %p')    }}
    {% endfor %}

有谁能告诉我如何让UTC时间[0]在浏览器中显示用户时区而不是UTC时间。在

我让用户输入他们的时区,所以我减去适当的小时数。在

但是,我不能通过我的方式来展示这个。在


Tags: the用户idchildflaskdatadate时间
1条回答
网友
1楼 · 发布于 2024-09-30 20:32:25

如果您有日期时间和用户的时区,您可以创建一个template filter,它接受一个datetime对象,进行所需的计算,然后打印字符串。在

如果您的问题实际上是关于将时区应用于一个简单的datetime对象,那么看看pytzrelevant section):

from datetime import datetime
from pytz import timezone

tz = timezone('saved_user_timezone')
dt = saved_time_from_db
locl_dt = tz.localize(dt)

要显示带有时区偏移量的日期时间,请尝试:

^{pr2}$

相关问题 更多 >