aleroot
aleroot

Reputation: 72686

c# compare datetime for day of month and time

I need to compare two datetime variable only for some criteria : Day of the month and time .

If the day of the month and the time is equal to another datetime then the expression return true, the comparison must not keep in mind of the month and the year of the two Datetime .

What's the best way to do this comparison ?

Thanks.

Upvotes: 0

Views: 2290

Answers (1)

dtb
dtb

Reputation: 217401

Compare the Day and the TimeOfDay properties of the two DateTime values:

bool areEqual = (d1.Day == d2.Day)
             && (Math.Abs((d1.TimeOfDay - d2.TimeOfDay).TotalSeconds) < 1.0);

Adjust for different time zones before comparing if necessary.

Upvotes: 3

Related Questions