datetime compare

  • Thread starter Thread starter ChrisB
  • Start date Start date
C

ChrisB

Hello,

I was wondering what the easiest way is to compare two DateTime objects and
not have the time components be included in the comparision.

For, example, if time1 = 10/01/07 9:00 am, and time2 = 10/1/07 10:00 am,
time1.CompareTo(time2) == 0 should evaluate to true.

Thanks,
Chris
 
Hello,

I was wondering what the easiest way is to compare two DateTime objects and
not have the time components be included in the comparision.

For, example, if time1 = 10/01/07 9:00 am, and time2 = 10/1/07 10:00 am,
time1.CompareTo(time2) == 0 should evaluate to true.

Thanks,
Chris

Hi Chris,
I would just compare the .Date properties:

DateTime dt1 = new DateTime(2007, 10, 1, 10, 0, 0);
DateTime dt2 = new DateTime(2007, 10, 1, 9, 0, 0);
TimeSpan ts = dt1.Date - dt2.Date;
bool sameDate = (ts == TimeSpan.Zero);

John
 
Thanks, Nicholas. That did the job.

Chris

Nicholas Paldino said:
Chris,

You would use time1.Date.CompareTo(time2.Date) == 0


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ChrisB said:
Hello,

I was wondering what the easiest way is to compare two DateTime objects
and not have the time components be included in the comparision.

For, example, if time1 = 10/01/07 9:00 am, and time2 = 10/1/07 10:00 am,
time1.CompareTo(time2) == 0 should evaluate to true.

Thanks,
Chris
 

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

Back
Top