I'm having trouble understanding why the same code is producing different results between .NET 6 and .NET 8 environments.
Specifically, I'm trying to understand why 'end' is not equal to 'newEnd'.
Here you can see a screenshots of the result from each environment:
.NET 6 - the string representation of newEnd
is '8/12/2025 11:59:59'
.
.NET 8 - the string representation of newEnd
is '8/12/2025 11:59:58'
.
This is the code:
DateTime start = new DateTime(2025, 08, 10, 0, 0, 0);
DateTime end = new DateTime(2025, 08, 12, 23, 59, 59);
double hoursDiff = end.Subtract(start).TotalHours;
DateTime newEnd = start.AddHours(hoursDiff);
As Panagiotis Kanavos pointed out earlier (in the staging phase) - the hoursDiff
value is the same between the two runtimes (it can be proved by printing the entire double using the 'N20' toString
parameter).
Therefore, the issue must be related to differences in the DateTime
object.
After further investigation of the NET DateTime
documentation, I have found that there's been a change in the rounding behavior inside the DateTime.AddHours
method which I believe is the source of the issue.
Source - https://learn.microsoft.com/en-us/dotnet/api/system.datetime.addhours?view=net-7.0