How to reverse the time slider?

I’m trying to create a slider that will take input from users on their sleep schedule. The usual sleep time being 22:00 to 06:00, I want the 22:00 to be on the left and 06:00 on the right. The user should be able to move the left slider to change his sleep_start time and the right slider to change the sleep_end time. The min value i.e. leftmost value I want to set is 2100, and max value will be 10:00, which will be the rightmost value.

How can I make this possible?

I tried this code,
sleep_time = st.slider( "Choose your sleep time:",min_value = (time(21,00)), max_value= (time(10,00)), value=(time(22, 00), time(7, 00)) )

but that doesn’t seem to be working.

Hey @Pranav3, you can try something like so:

import streamlit as st
from datetime import datetime, timedelta

today_10pm = datetime.combine(datetime.today(), datetime.min.time()) + timedelta(
    hours=22
)
tomorrow_6am = datetime.combine(datetime.today(), datetime.min.time()) + timedelta(
    days=1, hours=6
)

sleep_time = st.slider(
    "Choose your sleep time:",
    min_value=(today_10pm),
    max_value=(tomorrow_6am),
    format="hh:mm",
    value=(today_10pm, tomorrow_6am),
)

The trick is to use the format argument, but to use the real values inside of the slider.

1 Like

Thanks @Arthur1 . did work, only thing I’m now breaking my head about is why the start time didn’t show up as 22:00 instead of 10:00

hh is 12h format, use HH for 24h formatting instead (reference: Moment.js | Docs)