tags:

views:

202

answers:

2

Hi all,

How can I determine if a date is within 180 days?

I've been playing with DateTime.Compare below and using date1 +180 days (date1 is 28th August 2009 13:12)

DateTime.Compare(**date1**.AddDays(180), now) 

Is this correct?

Thanks

Jamie

+8  A: 
DateTime.Now.Subtract(dt1).Days <= 180
Developer Art
`dt2.Subtract(DateTime.Now).Days <= 180`
Vlad
@Developer Art: the order is important
Vlad
@Vlad: date1 lies in the past (28.08.2009), which is why it needs to be the argument, otherwise the Days returned will be negative.
Developer Art
+3  A: 
if (Math.Abs(DateTime.Now.Subtract(date1).Days) <= 180)
{
    ...
}

This will let you know if date1 is within 180 days IN EITHER DIRECTION of right now - past or future. If you only need into the past, go with Developer Art's answer.

Jesse C. Slicer