C# – Using Time Span to compare two time intervals

cdatetimetime

I want to compare between a certain time and the current time to check if it has exceeded a given period.

I think i should be using TimeSpan but i am not sure how. The two dates im comparing are DateTime objects.

 TimeSpan ts = TimeSpan.FromSeconds(_timeInterval);

 if(DateTime.UtcNow.Ticks - rex.LastFired.Ticks > ts.Ticks)
    // bla bla

Best Answer

Just compare the difference between your dates with a timespan.

var start = new DateTime(2013, 02, 15);
var now = DateTime.Now;

var oneWeek = new TimeSpan(7, 0, 0, 0);

if (now - start > oneWeek) {
    Console.Write("One week is passed since start date.");
} else {
    Console.Write("One week not yet passed since start date.");
}