Home All Groups Group Topic Archive Search About
Author
14 Feb 2006 4:16 PM
Simon Harvey
Hi everyone,

I need to be able to compare to dates to ensure that one is at least 1 day
greater than the other.

Im trying to do

if(toDate !> fromDate){
   // handle
}

This works but the problem is its comparing the datetime down to the minute
and second. I need to be accurate only to the date. Otherwise, a comparison
for two datetimes that are within the same date, but of different times will
give a false positive.

Can anyone tell me how to ensure that one datetime is at least one day greater
than the other?

Many thanks

Simon

Author
14 Feb 2006 4:25 PM
Vadym Stetsyak
Hello, Simon!

one way

DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
dt2 = dt2.AddDays(2);
TimeSpan span = dt2 - dt1;
if ( span.Days > 1 )
else

another one

or you can use DateTime.DayOfWeek or Day, or DayOfYear dependin on the scope of dates being compared
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
14 Feb 2006 5:47 PM
Jon Skeet [C# MVP]
Simon Harvey <notha***@hotmail.com> wrote:
Show quote
> I need to be able to compare to dates to ensure that one is at least 1 day
> greater than the other.
>
> Im trying to do
>
> if(toDate !> fromDate){
>    // handle
> }
>
> This works but the problem is its comparing the datetime down to the minute
> and second. I need to be accurate only to the date. Otherwise, a comparison
> for two datetimes that are within the same date, but of different times will
> give a false positive.
>
> Can anyone tell me how to ensure that one datetime is at least one day greater
> than the other?

if (toDate.Date > fromDate.Date)
{
    ...
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
16 Feb 2006 9:36 AM
Simon Harvey
Thanks guys!

AddThis Social Bookmark Button