Hint
Start by displaying the page title and asking the user to select a date and time. In NiceGUI, you need to use a separate label for each input:
ui.label('Select Date')
selected_date = ui.date(value=datetime.now().date())
ui.label('Select Time')
selected_time = ui.time(value='12:00')
Next, let the user choose two timezones from a dropdown list:
import pytz
timezones = pytz.all_timezones
ui.label('From Timezone')
from_tz = ui.select(timezones, value='UTC')
ui.label('To Timezone')
to_tz = ui.select(timezones, value='Asia/Tokyo')
Now when the user clicks a button, combine the selected date and time into a datetime
object, localize it with the from
timezone, and convert it using .astimezone()
:
from datetime import datetime
naive_dt = datetime.combine(selected_date.value, datetime.min.time()).replace(
hour=hour, minute=minute
)
localized = from_zone.localize(naive_dt)
converted = localized.astimezone(to_zone)
Then show the result using result.set_text(...)
. Click Show Solution on the project page if you need the full working code.