Comparing dates

S

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
 
V

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
 
J

Jon Skeet [C# MVP]

Simon Harvey said:
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)
{
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top