Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi How do I compare and get if time x is greater than time y?

EXAMPLE.

C#
DateTime x = DateTimeNow();
DateTime now = DateTime.Now;
TimeSpan diff = now - x;
System.Console.Write("File over stayed : time Differnece : " + diff); 

DateTime time = "00:30:00.000";
  
if (diff > time ) //This is where I get an Error
{
   System.Console.Write("File time too long : time Differnece : " + diff);
}
else
{
   System.Console.Write("File still new :"+diff);
}


Thanks in advance for your help.
Posted
Updated 22-Jan-13 8:06am
v3

DateTime is a point in time, where as TimeSpan is a duration.
With this in mind, think of it!

The difference between two points in time is a duration.
E.g. the difference between yesterday noon (a point in time) and today noon (a point in time) is 24 hours (a duration).

The difference between two durations is a duration again.
E.g. the difference between 5 minutes (duration) and 3 minutes (duration) is 2 minutes (duration).

Now you can elaborate yourself what it means to add/subtract durations/points in time to/from durations/points in time.
Some combinations may not be meaningful, though.

Finally, the DateTime and TimeSpan may not provide all possible operations from above.

Cheers
Andi
 
Share this answer
 
C#
DateTime first = GetFirstDate();
DateTime second = GetSecondDate();

if (first.Date.Equals(second.Date))
{
    // the dates are equal
}

in linq:

e => DateTime.Compare(e.FirstDate.Value, SecondDate) >= 0
 
Share this answer
 
v2
Comments
BobJanova 22-Jan-13 11:38am    
This doesn't answer the question. It also implies that DateTime.Equals and DateTime.Compare are the same thing, which they aren't.
Sergey Alexandrovich Kryukov 22-Jan-13 12:56pm    
Of course.
—SA
You are comparing a DateTime to a TimeSpan. I think you meant
TimeSpan time = new TimeSpan(0, 30, 0);
if(diff > time) ...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jan-13 12:57pm    
You can compare either DateTime or TimeSpan, apparently, with completely different meaning.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900