Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi How do I compare and get if time x is greater than time y?

EXAMPLE.

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 22-Jan-13 1:50am
Edited 22-Jan-13 9:06am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
BobJanova at 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 at 22-Jan-13 12:56pm
   
Of course.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You are comparing a DateTime to a TimeSpan. I think you meant
TimeSpan time = new TimeSpan(0, 30, 0);
if(diff > time) ...
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,578
1 OriginalGriff 8,420
2 Peter Leow 4,874
3 Kornfeld Eliyahu Peter 3,240
4 Maciej Los 2,356


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100