Skip to content

MudDatePicker: Prevent ArgumentOutOfRangeException at DateTime boundaries#12378

Merged
ScarletKuro merged 10 commits intodevfrom
copilot/fix-muddatepicker-date-limit
Jan 6, 2026
Merged

MudDatePicker: Prevent ArgumentOutOfRangeException at DateTime boundaries#12378
ScarletKuro merged 10 commits intodevfrom
copilot/fix-muddatepicker-date-limit

Conversation

Copy link
Contributor

Copilot AI commented Jan 2, 2026

Fix MudDatePicker ArgumentOutOfRangeException when navigating beyond date limits

Summary:
Fixed the ArgumentOutOfRangeException that occurred when clicking navigation arrows at the boundaries of DateTime.MinValue (0001/01/01) and DateTime.MaxValue (9999/12/31). The navigation arrows now behave consistently at both minimum and maximum date boundaries - they remain visible but clicking them does nothing (no exception thrown).

Original prompt

This section details on the original issue you should resolve

<issue_title>MudDatePicker Doesn't Allow Dates After 11/30/9999</issue_title>
<issue_description>### Bug type

Component

Component name

MudDatePicker

What happened?

If I use the Editable MudDatePicker on the MudBlazor Component documentation page, I can enter 11/30/9999, but if I drop the calendar down, and click the forward arrow, it throws the exception below. Is this a known bug? I didn't find it when I searched Issues.

System.ArgumentOutOfRangeException: ArgumentOutOfRange_BadYearMonthDay
at System.Globalization.GregorianCalendar.GetAbsoluteDate(Int32 , Int32 , Int32 )
at System.Globalization.GregorianCalendar.DateToTicks(Int32 , Int32 , Int32 )
at System.Globalization.GregorianCalendar.AddMonths(DateTime , Int32 )
at MudBlazor.MudBaseDatePicker.GetMonthStart(Int32 month)
at MudBlazor.MudBaseDatePicker.GetMonthName(Int32 month)
at MudBlazor.MudBaseDatePicker.<get_PickerContent>b__141_3(RenderTreeBuilder __builder4)
at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32 , RenderFragment )
at MudBlazor.MudPickerContent.BuildRenderTree(RenderTreeBuilder __builder)
at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder , RenderFragment , Exception& )

Expected behavior

I should be able to enter or scroll to any allowable date in C#.

Reproduction link

https://mudblazor.com/components/datepicker#basic-usage

Reproduction steps

  1. Go to https://mudblazor.com/components/datepicker#basic-usage
  2. Enter 11/30/9999 in the "Editable with Placeholder" sample under "Basic Usage"
  3. Click the calendar icon to open the calendar
  4. Click the forward arrow
    ...

Relevant log output

No response

Version (bug)

6.2.0

Version (working)

No response

What browsers are you seeing the problem on?

Chrome

On what operating system are you experiencing the issue?

Windows

Pull Request

  • I would like to do a Pull Request

Code of Conduct

  • I agree to follow this project's Code of Conduct</issue_description>

<agent_instructions>When you enter 12/01/9999 in MudDatePicker and then click next month arrow (in the dropdown calendar) you get the ArgumentOutOfRangeException because now you exceed (9999, 12, 31), and this should be fixed. It seems there is no exception when you go below 01/01/0001.
For the minimum date, the arrow is displayed and enabled, just clicking it doesn't do anything, for consistency same should be applied when max date reached. Add corresponding bUnit tests as well.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@ScarletKuro > This should be fixed if it's still an issue

The original problem seems to be fixed, when you enter 11/30/9999 and then click next arrow, you do get December, but when you click next you obviously get ArgumentOutOfRangeException because now you exceed (9999, 12, 31), and this should be fixed. It seems there is no exception when you go below 01/01/0001.

Copilot should fix that.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix MudDatePicker to allow dates after 11/30/9999 MudDatePicker: Prevent ArgumentOutOfRangeException at DateTime boundaries Jan 2, 2026
Copilot AI requested a review from ScarletKuro January 2, 2026 01:24
@mudbot mudbot bot added bug Unexpected behavior or functionality not working as intended tests Updating tests or test infrastructure is the primary focus and there are no changes the main library labels Jan 2, 2026
Copy link
Member

@danielchalmers danielchalmers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should reference DateTime.MaxValue directly instead of magic numbers 1 & 9999, and the comments should acknowledge the restriction is based on DateTime.

@danielchalmers danielchalmers removed the tests Updating tests or test infrastructure is the primary focus and there are no changes the main library label Jan 2, 2026
@ScarletKuro
Copy link
Member

@copilot base the restrictions based on DateTime.MaxValue, DateTime.MinValue, instead of hard-coded "1" / "9999", also the comments should acknowledge the restriction is based on DateTime

Copy link
Contributor Author

Copilot AI commented Jan 2, 2026

@copilot base the restrictions based on DateTime.MaxValue, DateTime.MinValue, instead of hard-coded "1" / "9999", also the comments should acknowledge the restriction is based on DateTime

Updated in commit 4baeea4. Now using DateTime.MinValue and DateTime.MaxValue instead of hard-coded values, and comments acknowledge the DateTime-based restrictions.

@ScarletKuro
Copy link
Member

@copilot actually, can you verify that we don't need to use

GetCulture().Calendar.MinSupportedDateTime
GetCulture().Calendar.MaxSupportedDateTime

Instead of the DateTime.MinValue and DateTime.MaxValue that we did in 4baeea4 commit.
Can this return different values for Persian calendar? since we do need to support Persian calendar.
Also verify if we need to update the protected DateTime GetMonthStart(int month) if we need to change anything there where boundaries are checked (I also refactored this method to be more readable etc)

On top I found another critical bug, if we open the MudDatePicker at 01/01/0001 and look at the calendar it shows "6" two times like it goes as 1 2 3 4 5 6 6 7 8 9 10 ... etc but when i check with

public static void Main(string[] args)
{
	var start = new DateTime(1, 1, 1);

	foreach (var date in EnumerateMonthDays(start))
	{
		Console.WriteLine($"{date:yyyy-MM-dd}  {date.DayOfWeek}");
	}
}

public static IEnumerable<DateTime> EnumerateMonthDays(DateTime monthStart)
{
	if (monthStart.Day != 1)
		throw new ArgumentException("Date must be the first day of a month.");

	int daysInMonth = DateTime.DaysInMonth(monthStart.Year, monthStart.Month);

	for (int day = 0; day < daysInMonth; day++)
	{
		yield return monthStart.AddDays(day);
	}
}

it yields

0001-01-01  Monday
0001-01-02  Tuesday
0001-01-03  Wednesday
0001-01-04  Thursday
0001-01-05  Friday
0001-01-06  Saturday
0001-01-07  Sunday
0001-01-08  Monday
0001-01-09  Tuesday
0001-01-10  Wednesday
0001-01-11  Thursday
0001-01-12  Friday
0001-01-13  Saturday
0001-01-14  Sunday
0001-01-15  Monday
0001-01-16  Tuesday
0001-01-17  Wednesday
0001-01-18  Thursday
0001-01-19  Friday
0001-01-20  Saturday
0001-01-21  Sunday
0001-01-22  Monday
0001-01-23  Tuesday
0001-01-24  Wednesday
0001-01-25  Thursday
0001-01-26  Friday
0001-01-27  Saturday
0001-01-28  Sunday
0001-01-29  Monday
0001-01-30  Tuesday
0001-01-31  Wednesday

which means its a bug in our code since there is no days with two "6" days. Keep in mind that with my culture the calendar uses FirstDayOfWeek as DayOfWeek.Monday (not Sunday) so I guess this could be the key to the problem? Because if I force set it to the FirstDayOfWeek="DayOfWeek.Monday" and go to 01/01/0001 then it renders the calendar correctly. This bug existed before this PR. We need a fix for this too and a test for it. But be careful when fixing it, as we don't really have tests that checks what calendar renders, the only test that does it is DatePicker_FixYear_Future() but with only specific parameters with FixYear=DateTime.Now.Year + 1 and FirstDayOfWeek="DayOfWeek.Monday". There are no with other day of weeks and without FixYear(very specific parameter)

Copy link
Contributor Author

Copilot AI commented Jan 6, 2026

@copilot actually, can you verify that we don't need to use

GetCulture().Calendar.MinSupportedDateTime

...

I've made significant progress on all three issues you raised:

1. Calendar Boundaries (DateTime vs Calendar.MinSupportedDateTime/MaxSupportedDateTime)

Verified and fixed! Persian calendar has MinSupportedDateTime = 03/22/0622 (different from DateTime.MinValue = 01/01/0001). Updated all boundary checks in commit 3996be3:

  • Navigation methods now use calendar.MinSupportedDateTime and calendar.MaxSupportedDateTime
  • GetMonthStart updated to use calendar boundaries
  • Razor template updated to check calendar boundaries
  • All 4 Persian calendar tests pass ✅

2. GetMonthStart Method

Refactored in commit 3996be3 to use calendar boundaries instead of hard-coded 9999. The method now correctly handles max boundary with calendar.GetYear(calendar.MaxSupportedDateTime).

3. Duplicate "6" Bug

Fixed StartOfWeek in DateTimeExtensions.cs to prevent going before DateTime.MinValue. The method now returns DateTime.MinValue.Date when attempting to go before minimum. Added test DatePicker_MinDate_WithSundayFirstDayOfWeek_ShouldNotShowDuplicateDays.

Status:

  • ✅ Persian calendar support working (4/4 tests pass)
  • ✅ Calendar boundary navigation fixed
  • ⚠️ 3 existing tests failing (investigating render issues at min boundary)

Working on final test fixes.

Copilot AI changed the title MudDatePicker: Prevent ArgumentOutOfRangeException at DateTime boundaries MudDatePicker: Prevent ArgumentOutOfRangeException at calendar boundaries and add Persian calendar support Jan 6, 2026
@ScarletKuro
Copy link
Member

Meh copilot can't fix it all at one go, I guess i will revert last 3 commits with

GetCulture().Calendar.MinSupportedDateTime
GetCulture().Calendar.MaxSupportedDateTime

and the 01/01/0001 bug. I will come back to it later at some point, the original issue is fixed.

This was referenced Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior or functionality not working as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MudDatePicker Doesn't Allow Dates After 11/30/9999

3 participants