An app allows a user to define an arbitrary set of days like so:
- Day 1 – Pick flowers
- Day 2 – Have coffee
- Day 3 – Go swimming
This set of days may have only one entry or it may have many. Suppose that this cycle of days begins on March 23, 2010.
Is there a general algorithm that can determine which day in a cycle corresponds to a given calendar day?
On March 24th, the algorithm should return Day 2, March 25th should be Day 3, March 26th should be Day 1, and so on…
More specifically, I am writing this code for a Cocoa application. So, barring the existence of a more general technique can the Calendar classes help me?
daysnow contains the number of days which have elapsed since the start date. This can be used as an index (zero-based) into your array of activities. You can use the modulus operator with the length of the cycle as the dividend to compute indices after the first transit around the cycle.