471,327 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 software developers and data experts.

Comparing dates

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
Feb 14 '06 #1
6 2192
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
Feb 14 '06 #2
Simon Harvey <no******@hotmail.com> wrote:
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
Feb 14 '06 #3
On Tue, 14 Feb 2006 08:16:33 -0800, Simon Harvey
<no******@hotmail.com> wrote:

-> 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
}
Feb 15 '06 #4
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

Feb 15 '06 #5
On 15 Feb 2006 06:18:35 -0800, "Jon Skeet [C# MVP]" <sk***@pobox.com>
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 :).

Feb 16 '06 #6
Thanks guys!
Feb 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike N. | last post: by
2 posts views Thread by Duppypog | last post: by
2 posts views Thread by Philip Townsend | last post: by
2 posts views Thread by Manny Chohan | last post: by
2 posts views Thread by ameshkin | last post: by
12 posts views Thread by colincolehour | last post: by
4 posts views Thread by cheryl | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.