Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a timespan in negative. C#

The title says it all. I'm subtracting two DateTimes and I want to see if the returned Timespan is negative. How do I do this?

// dt1 and dt2 are DateTimes.
TimeSpan ts = dt1.Subtract(dt2);
ts.isNegative() // or something like that
like image 811
Jonathan S. Avatar asked Nov 23 '25 17:11

Jonathan S.


1 Answers

TimeSpan has a Compare method on it, or you can do < TimeSpan.Zero or you could have just compared the two DateTimes in the first place and skipped creating the TimeSpan entirely.

like image 159
Ian Mercer Avatar answered Nov 26 '25 11:11

Ian Mercer