# Date range selection from input with custom dates and quarter, monthly, yearly etc

**URL:** <https://discuss.streamlit.io/t/date-range-selection-from-input-with-custom-dates-and-quarter-monthly-yearly-etc/87559>\
**Category:** Using Streamlit\
**Tags:** date\_input, discussion\
**Created:** [December 12, 2024, 10:23am UTC](https://discuss.streamlit.io/t/date-range-selection-from-input-with-custom-dates-and-quarter-monthly-yearly-etc/87559 "2024-12-12T10:23:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CharanPatgar](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/charanpatgar/32/53293_2.png) [@CharanPatgar](https://discuss.streamlit.io/u/CharanPatgar)\
**Post date:** [December 12, 2024, 10:23am UTC](https://discuss.streamlit.io/t/date-range-selection-from-input-with-custom-dates-and-quarter-monthly-yearly-etc/87559/1 "2024-12-12T10:23:43Z")

</div>

I have an application and it has a date input, for user friendly input i want it to have multiple options in the date input, i.e today, yesterday, custom range, last week . last month, last year, last quarter etc.  
Refer the image, How can i achieve the same in streamlit?

 ![image_2024_12_11T16_43_41_881Z (1)](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cfdebb7620d685c2c29ee3d17e060c093d71ec05.png)

---

<div class="post-metadata">

**Author:** ![Faltawer](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/faltawer/32/46117_2.png) [@Faltawer](https://discuss.streamlit.io/u/Faltawer)\
**Post date:** [December 12, 2024, 10:55am UTC](https://discuss.streamlit.io/t/date-range-selection-from-input-with-custom-dates-and-quarter-monthly-yearly-etc/87559/2 "2024-12-12T10:55:20Z")

</div>

It not as nice as your example, but here a begining of hint i would do :

```auto
        from datetime import datetime, timedelta

        # Function to get the date range for "Last Week", "Last Month", etc.
        def get_date_range(option):
            today = datetime.today()
            if option == "Today":
                start_date = today
                end_date = today
            elif option == "Yesterday":
                start_date = today - timedelta(days=1)
                end_date = today - timedelta(days=1)
            elif option == "Last Week":
                start_date = today - timedelta(weeks=1)
                end_date = today - timedelta(days=1)
            elif option == "Last Month":
                start_date = today.replace(day=1) - timedelta(days=1)
                start_date = start_date.replace(day=1)
                end_date = today.replace(day=1) - timedelta(days=1)
            elif option == "Last Year":
                start_date = today.replace(year=today.year - 1, month=1, day=1)
                end_date = today.replace(year=today.year - 1, month=12, day=31)
            elif option == "Last Quarter":
                if today.month <= 3:
                    start_date = today.replace(year=today.year - 1, month=10, day=1)
                    end_date = today.replace(year=today.year - 1, month=12, day=31)
                elif today.month <= 6:
                    start_date = today.replace(month=1, day=1)
                    end_date = today.replace(month=3, day=31)
                elif today.month <= 9:
                    start_date = today.replace(month=4, day=1)
                    end_date = today.replace(month=6, day=30)
                else:
                    start_date = today.replace(month=7, day=1)
                    end_date = today.replace(month=9, day=30)
            else:
                start_date, end_date = None, None

            return start_date, end_date

        # Streamlit selectbox for date range options
        option = st.selectbox("Choose a Date Range",
                              ["Today", "Yesterday", "Last Week", "Last Month", "Last Year", "Last Quarter",
                               "Custom Range"])

        # Get the start and end date based on the selected option
        start_date, end_date = get_date_range(option)

        start_date = st.date_input("Start date",value=(start_date, end_date))

```

---

<div class="post-metadata">

**Author:** ![CharanPatgar](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/charanpatgar/32/53293_2.png) [@CharanPatgar](https://discuss.streamlit.io/u/CharanPatgar)\
**Post date:** [December 12, 2024, 11:48am UTC](https://discuss.streamlit.io/t/date-range-selection-from-input-with-custom-dates-and-quarter-monthly-yearly-etc/87559/3 "2024-12-12T11:48:38Z")

</div>

Thank you, but this wont give me the custom and some other details anyways it would help me build the one.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [June 10, 2025, 11:48am UTC](https://discuss.streamlit.io/t/date-range-selection-from-input-with-custom-dates-and-quarter-monthly-yearly-etc/87559/4 "2025-06-10T11:48:48Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
