Date algorithm over my head

  • Thread starter Thread starter Mike Teague
  • Start date Start date
M

Mike Teague

I am a new programmer and have been assigned a task of
creating a C# (I'm reading my C# for dummies book)
algorithm of breaking a year into 26 two week periods.
Within those 26 periods, I have to be able to know the
beginning and ending of the period (period is 2 weeks).

You might ask why I have to do this? Salesmen budgets
are broken into 26 two week periods every year. If the
salesmen request more product, I have to check if his
request is granted if that would put him over his
budgeted alotment for that period (1of 26 for the year).

I figure my logic is like this: They request more
product, I take today's date and figure which of the 26
periods of this year (spans of 2 weeks) that today falls
in, let's say it falls in period 12, I then go look in
the database for period 12 and see what the salesmen was
budgeted and see if his request > budgeted.

I think my algorithm has to start at Jan. 1st of what
ever year it is and break the year into 26 two week
periods putting the beginning date of the each 2 week
period and the ending date of the 2 week period in an
array. Once the array is created, I have to take today's
date and find out which period it is.

I appreciate any feedback that could give a clue on how
to accomplish this so my first programming job is not my
last.

Thanks,
Mike
 
....
I think my algorithm has to start at Jan. 1st of what
ever year it is and break the year into 26 two week
periods putting the beginning date of the each 2 week
period and the ending date of the 2 week period in an
array. Once the array is created, I have to take today's
date and find out which period it is.
Why can't you just calculate the number of days from Jan 1, and the use the
modulo operator to get the period number?

Somethig like:
int GetPeriod(DateTime d)
{
int days = d.DayOfYear;
int period = days % 26;
return period;
}
 
....
I think my algorithm has to start at Jan. 1st of what
ever year it is and break the year into 26 two week
periods putting the beginning date of the each 2 week
period and the ending date of the 2 week period in an
array. Once the array is created, I have to take today's
date and find out which period it is.
Why can't you just calculate the number of days from Jan 1, and then divide
by 14 to get the period number?
Then add 1 if you want the periods to start from 1 and not 0.

Somethig like:
int GetPeriod(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
}
 
Back
Top