```Peter Bailey wrote:
> Mohit Sindhwani wrote:
>
>> Anyway, if you have:
>>
>> now = DateTime.now
>> this_month = now.month
>> this_budget_fencepost = \\\ based on what we discussed earlier ///
>>
>> then, you can find the budget fencepost for today using that..
>>
>> If your budget posts are every 4 weeks from the last Sunday of January,
>> you should be able to get the number of days since the first
>> num_of_days = this_budget_fencepost - first_fencepost
>> fencepost_number = num_of_days/ 28
>>
>> You need to put in some check if the date is earlier than the first
>> fencepost and check the numbers it actually produces, but roughly that
>> should do it for you.
>>
>> Cheers,
>> Mohit.
>> 9/26/2007 | 10:59 PM.
>>
>
> I basically just did a comparsion for the time.now between each of the
> budget periods. If between 0 and 1, then budget1; if between 1 and 2,
> then budget2, and so on. A little painful, but it's done with about 12
> lines of code. I can live with that. Thanks.
>

Whatever works, I guess :)

I've always preferred a calculation to 12 comparisons.  That way, it
scales well when the periods change - you just need to change a division
factor :)  but like I said, whatever works for you!

By the way, are you calculating all 12 budget fenceposts every time you
need to find out which one the current day falls in?

There are numerous optimizations possible...
* You could consider putting all the budget posts in an array and
iterating over the array using a smaller loop code.
* You could check what month it is and check only the budget posts
(month-1) and (month) and (month+1).  If I'm not wrong, no more than 3
budget periods will fall in the same month - it's a bit convoluted though :P
* If you are in fact creating the budget fenceposts as you go along, you
don't need to generate all 12.. just generate till you find the one you
are looking for.

Anyway, there are a few options - enjoy!

Cheers,
Mohit.
9/26/2007 | 11:38 PM.

```