Working with timezones

In general, working with timezones can be tricky. Your Streamlit app users are not necessarily in the same timezone as the server running your app. It is especially true of public apps, where anyone in the world (in any timezone) can access your app. As such, it is crucial to understand how Streamlit handles timezones, so you can avoid unexpected behavior when displaying datetime information.

Streamlit always shows datetime information on the frontend with the same information as its corresponding datetime instance in the backend. I.e., date or time information does not automatically adjust to the users' timezone. We distinguish between the following two cases:

When you provide a datetime instance without specifying a timezone, the frontend shows the datetime instance without timezone information. For example (this also applies to other widgets like st.dataframe):

Python
import streamlit as st
from datetime import datetime

st.write(datetime(2020, 1, 10, 10, 30))
# Outputs: 2020-01-10 10:30:00

Users of the above app always see the output as 2020-01-10 10:30:00.

When you provide a datetime instance and specify a timezone, the frontend shows the datetime instance in that same timezone. For example (this also applies to other widgets like st.dataframe):

Python
import streamlit as st
from datetime import datetime
import pytz

st.write(datetime(2020, 1, 10, 10, 30, tzinfo=pytz.timezone("EST")))
# Outputs: 2020-01-10 10:30:00-05:00

Users of the above app always see the output as 2020-01-10 10:30:00-05:00.

In both cases, neither the date nor time information automatically adjusts to the users' timezone on the frontend. What users see is identical to the corresponding datetime instance in the backend. To show date or time information in the timezone of the user viewing the app, use st.context.timezone or st.context.timezone_offset to get the timezone of the user's browser. Then, convert your datetime instances to that timezone before displaying them.

forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.