Comparing dates

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
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
 
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
 
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)
{
...
}
 
On Tue, 14 Feb 2006 08:16:33 -0800, 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
->


Maybe ?


TimeSpan oneDay = new TimeSpan(1, 0, 0, 0);

if (toDate >= fromDate + oneDay)
{
// Code here
}
 
Michael H wrote:

TimeSpan oneDay = new TimeSpan(1, 0, 0, 0);

if (toDate >= fromDate + oneDay)
{
// Code here
}

Unless I've misunderstood something, I believe the original purpose was
to compare the dates - so "just before midnight" should count as a
different date to "just after midnight", even though it wouldn't go
inside your "if" statement.

Jon
 
On 15 Feb 2006 06:18:35 -0800, "Jon Skeet [C# MVP]" <[email protected]>
wrote:

-> Michael H wrote:
->
-> <snip>
->
-> > TimeSpan oneDay = new TimeSpan(1, 0, 0, 0);
-> >
-> > if (toDate >= fromDate + oneDay)
-> > {
-> > // Code here
-> > }
->
-> Unless I've misunderstood something, I believe the original
purpose was
-> to compare the dates - so "just before midnight" should count as a
-> different date to "just after midnight", even though it wouldn't
go
-> inside your "if" statement.
->
-> Jon


Jon,

Yep, I wasn't exactly sure what he was asking specifically. Is a "1
day" difference 24 hrs or the day's label, like Feb 12 or Feb 13 :).
 
Back
Top