Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with DateTime and leap year #111021

Closed
Tragen opened this issue Jan 1, 2025 · 4 comments
Closed

Bug with DateTime and leap year #111021

Tragen opened this issue Jan 1, 2025 · 4 comments

Comments

@Tragen
Copy link

Tragen commented Jan 1, 2025

Description

If you add one year to 2024-02-29, the expected result differs from the actual result

Reproduction Steps

Create a console application

		DateTime d = new DateTime(2024, 02, 29);
		d = d.AddYears(1);
		Console.WriteLine(d.ToString());

Expected behavior

It should print the first of March 2025 and not the 28th of February 2025.
VB6, JavaScript and more languages do it correctly.

Actual behavior

.NET outputs 28th of February 2025.

Regression?

No response

Known Workarounds

No response

Configuration

Tested on .NET 4.8 until .NET 9 on Windows x64 and in https://dotnetfiddle.net/Hxy8gt

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 1, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 1, 2025
@huoyaoyuan huoyaoyuan added area-System.DateTime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jan 1, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-datetime
See info in area-owners.md if you want to be subscribed.

@huoyaoyuan
Copy link
Member

Then handling of leap date is very arbitrarily chosen and subjective. It also applies to AddMonth on month ends. There's no behavior that's more "correct", as long as they are consistent.

@Clockwork-Muse
Copy link
Contributor

FWIW, Java also uses this "incorrect" behavior:

This method adds the specified amount to the years field in three steps:

 1. Add the input years to the year field
 2. Check if the resulting date would be invalid
 3. Adjust the day-of-month to the last valid day if necessary

For example, 2008-02-29 (leap year) plus one year would result in the invalid date 2009-02-29 (standard year). Instead of returning an invalid result, the last valid day of the month, 2009-02-28, is selected instead.

Note that, while the behavior is technically arbitrary, retaining the same month is actually beneficial for "striding" implementation use.

That aside, for this and similar month-to-month behavior, you should usually avoid end-of-month dates when possible, and I personally tend to regard any math on such values other than +/- days as concerning. You're usually better off working with start-of-month values.
If this fell out of real-world code, what was this value intended to represent?

@Clockwork-Muse
Copy link
Contributor

As designed.
Even if we decided this was actually a bug, the change would be so massively breaking it would be rejected anyways.

@Clockwork-Muse Clockwork-Muse closed this as not planned Won't fix, can't repro, duplicate, stale Jan 1, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants