tags:

views:

294

answers:

1

What's the most efficient way to check if a date falls between a repetition of a span of time using the c# date class.

E.g., you have the span 01/01/2009 1:00:00 am to 01/06/2009 5:00pm which repeats every x number of years, months, or weeks, etc.

And I want to check if variable date x falls between any recurrence

I read Martin Fowler's paper on temporal expressions but all his examples are of single day events not ones that can span multiple days.

+2  A: 

Following condition must be true:

(time - begin) mod period < (end - begin),

provided that period > (begin - end)

All variables should be expressed in same units (like, seconds or whatever precision you need)

Quassnoi
Unfortunately most periods involving datetimes are not constant (months are 28-31 days long, years are 365/366 days, etc...)
Eclipse
This is a very neat method which will work fine for numerically constant distances between the intervals (e.g. 7 days, 30 days, 365 days) but if date driven rules are to be followed, e.g 'every month', 'every year' then something else is required (see my comment on the question).
AdamRalph