12,826,440 members (25,782 online)
Rate this:
See more:
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);
}```

Posted 22-Jan-13 1:50am
Updated 22-Jan-13 9:06am
v3

Rate this:

## 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
Rate this:

## 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```
v2
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
Rate this:

## Solution 2

You are comparing a DateTime to a TimeSpan. I think you meant
```TimeSpan time = new TimeSpan(0, 30, 0);
if(diff > time) ...```
Sergey Alexandrovich Kryukov 22-Jan-13 12:57pm

You can compare either DateTime or TimeSpan, apparently, with completely different meaning.
—SA

Top Experts
Last 24hrsThis month
 OriginalGriff 430 ppolymorphe 150 Dave Kreskowiak 135 Jochen Arndt 128 Graeme_Grant 103
 OriginalGriff 5,612 Graeme_Grant 3,935 Karthik Bangalore 3,681 ppolymorphe 2,789 Jochen Arndt 2,739