Using Time Span to compare two time intervals
14,556
Solution 1
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.");
}
Solution 2
if(DateTime.UtcNow - rex.LastFired > ts)
...
Solution 3
Why not use the DateTime.Compare ?
http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx
EDIT: you ll first have to use TimeSpan.Substract or DateTime.Substract, then you can use the appropiate .Compare method.
Author by
Marwan Tushyeh
Updated on June 05, 2022Comments
-
Marwan Tushyeh almost 2 years
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
-
Alex Filipovici about 11 yearsI think you have to use
rex.LastFired
(without.Ticks
). -
Thomas Levesque about 11 years@AlexFilipovici, yes, that's what I intended to write... thanks