Home > Software design >  How to check if a timespan in negative. C#
How to check if a timespan in negative. C#

Time:09-14

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

CodePudding user response:

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.

  • Related