I know this has been asked before, and I'm still pulling my hair out trying to figure this one out. I've tried pytz, dateutil, and now flask_moment. Still having problems converting a MySQL table datetime, which is UTC, to display in the jinja2 template as local time, specifically UTC - 05:00 (EST).
My jinja2 for loop looks like this:
{% for data in items %}
...
<td>{{data.loggedInBy}}</td>
<td>{{data.timeIn.strftime('%I:%M %p')}}</td>
...
{% endfor %}
I'm new to python/flask/jinja, so go easy on me, please. The documentation is pretty confusing for a noob such as myself. Can someone please walk me through getting my MySQL table to display the times in local timezone?
I feel like this is close, but getting errors with it. In init.py, I had:
from pytz import timezone
def datetimefilter(value, format='%I:%M %p'):
tz = timezone('US/Eastern')
dt = value
local_dt = tz.localize(dt)
local_dt.replace(hour=local_dt.hour + int(local_dt.utcoffset().total_seconds() / 3600))
return local_dt.strftime(format)
flask_app.jinja_env.filters['datetimefilter'] = datetimefilter
jinja template had:
{% for data in items %}
...
<td>{{data.loggedInBy}}</td>
<td>{{data.timeIn | datetimefilter }}</td>
...
{% endfor %}
But this was giving me "ValueError: hour must be in 0..23" From line:
local_dt.replace(hour=local_dt.hour + int(local_dt.utcoffset().total_seconds() / 3600))
Thanks in advance!