Date Range Selection Across Financial Years

I’m using a date input to select a date range between financial years, but I’m encountering a problem: I cannot jump to the next year using the year dropdown. I discovered the issue is due to the financial year running from April to March, without any overlapping months. Is there a workaround to avoid this complication so that users can choose the date range seamlessly?

I will attach a simple reproducible code

import datetime
from collections import defaultdict

import streamlit as st

date_strings = [
    "2024-01-22",
    "2024-01-11",
    "2024-01-10",
    "2023-12-28",
    "2023-12-04",
    "2023-12-03",
    "2023-07-12",
    "2023-07-11",
    "2023-12-27",
    "2023-07-10",
    "2024-01-12",
    "2023-12-17",
    "2023-12-16",
    "2023-12-15",
    "2023-12-14",
    "2023-06-08",
    "2023-06-09",
    "2023-07-24",
    "2023-07-25",
    "2023-07-22",
    "2023-04-20",
    "2023-07-23",
    "2023-07-21",
    "2024-01-01",
    "2023-12-01",
    "2023-12-05",
    "2023-12-02",
    "2023-12-06",
    "2023-06-19",
    "2023-07-14",
    "2023-04-30",
    "2023-07-13",
    "2024-01-31",
    "2024-01-26",
    "2023-06-17",
    "2023-06-05",
    "2023-07-31",
    "2023-12-23",
    "2023-04-24",
    "2024-01-03",
    "2023-12-08",
    "2023-11-09",
    "2023-04-13",
    "2023-07-16",
    "2023-12-12",
    "2023-07-20",
    "2023-09-09",
    "2023-12-10",
    "2023-07-28",
    "2023-09-07",
    "2024-01-15",
    "2024-01-13",
    "2023-12-18",
    "2023-10-31",
    "2023-07-26",
    "2023-06-07",
    "2023-09-18",
    "2023-12-21",
    "2024-01-24",
]

dates = [
    datetime.datetime.strptime(date_str, "%Y-%m-%d").date() for date_str in date_strings
]


def financial_year(date):
    start_of_year = datetime.date(date.year, 4, 1)
    if date < start_of_year:
        return date.year - 1
    return date.year

financial_years = defaultdict(list)
for date in dates:
    fy = financial_year(date)
    financial_years[fy].append(date)

for year in financial_years:
    financial_years[year] = sorted(financial_years[year])


st.title("Financial Year Date Range Picker")

selected_year = st.selectbox("Select Financial Year", list(financial_years.keys()))

if selected_year:
    available_dates = financial_years[selected_year]
    min_date, max_date = min(available_dates), max(available_dates)

    selected_dates = st.date_input(
        "Select your date range",
        (min_date, min_date + datetime.timedelta(days=7)),
        min_date,
        max_date,
        format="DD/MM/YYYY",
    )
    st.write(f"Selected Dates: {selected_dates}")

Just use the right button to select 2024 dates.

Hi @ferdy, thanks for the reply. Yes, we can proceed month by month to reach next year, but is there a way to directly jump to the next year using the year dropdown? Currently, since there are no similar months in the next year, it is disabled. Is there an option to select 2024 and automatically switch to January?