I'm having a hard time understanding why this is happening or even if this should be happening. If I calculate the TimeSpan
between two DateTime
objects (same date, different times) and compare it to the same calculation using two TimeOnly
objects I get different results.
var start = new DateTime(1, 1, 1, 14, 0, 0);
var end = new DateTime(1, 1, 1, 10, 0, 0);
Console.WriteLine(end - start); // Prints -4 hours
However...
var start = new TimeOnly(14, 0, 0);
var end = new TimeOnly(10, 0, 0);
Console.WriteLine(end - start); // Prints 20 hours???
Isn't the span between starting at 2pm and ending at 10am always a span of -4 hours? Interestingly enough if I take the second one and do Console.WriteLine(end.ToTimeSpan() - start.ToTimeSpan());
I end up with -4 hours.
This feels like an error on TimeOnly
's part but I don't know. Here is a fiddle I did comparing results between NodaTime
, System.DateTime
, converting System.TimeOnly
to TimeSpan
, and System.TimeOnly
.
TimeSpan
itself. – ParodicTimeSpan
as well? It must keep reference of the day (since you can doTimeSpan.FromDays(int)
). So when I doTimeSpan.FromHours(14) - TimeSpan.FromHours(10)
I end up with-4
. – Wellesz