Figuring the day

  • Thread starter Thread starter Darin Browne
  • Start date Start date
D

Darin Browne

We figure budgets in 26 14 days periods in a year. I
currently can take a date and calculate which of those 26
periods that date falls in. Now I have to figure out
what is beginning and ending date of that period.

Here is how the period is figured:
public int GetBudgetPeriod(DateTime d)
{
int days = d.DayOfYear;
int period = (days-1) / 14; // -1 to make sure the
14'th day is in period 1
return period+1; // add 1 to make
periods start at 1
}

Nothing is working for me on figuring out the beginning
and ending dates of the period.

Any suggestions?

Thanks.
 
Darin,

That is easy enough. If you have the beginning of the year in the date
then you can do the following.

Once you have the period, you can take the beginning of the year, and
add 14 days for each period (minus one). So, you would have this:

// Get the days from the beginning of the year that the period falls in.
int pintStartDaysToAdd = (pintPeriods - 1) * 14;

This would give you 0 for the first period, 1 for the second, and so on,
and so on.

Then, to get the end, you would just add 14 to the result to get the end
of the period and subtract one:

// Add 14 days to the beginning of the period.
int pintEndDaysToAdd = pintStartDaysToAdd + 13;

Now, add both of those values to the beginning of the year to get the
beginning and end date (using the DateTime and TimeSpan structures).

Hope this helps.
 
Back
Top