Shouldn't be too hard. Of course, you begin by using DateTime.Subtract to
get the TimeSpan between the 2 dates.
The tricky part is calculating the fractions of pay periods that occur. This
is difficult because months are not the same number of days long. Some are
30, some are 31, and February and leap year are special cases. So, you have
to figure out your rules for determining what constitutes a "full pay
period." Is it exactly half of the average length of a month in
double-precision floating point days? Or do you calculate it on a
month-by-month basis, based upon the first 15 days of the month, and the
remaining number of days in that month? How you will calculate this depends
upon the business rule here.
Except for the first and last month, all months between them will contain
exactly 2.0 pay periods, since every month in the Calendar is more than 15
days long, even during leap year! ;-) So, your only real task is figuring
out the "left-overs," the days after the day of the month of the first
DateTime, and the days before the day of the month of the second DateTime.
For example, if the beginning and end DateTimes were Feb 1, 2006 and May 28,
2006, there will be 2 full months between the 2 Dates: March and April.
These 2 months constitute 2 * 2.0D (4.0D) pay periods.
So, you need to determine the Day of the month for the beginning and ending
DateTimes. Again, depending upon your business rule for determining the
length of the second pay period, the remainder will vary. If, for example,
you determine that a pay period constitues an average of 15.345 days (I made
up that number), you start with the first date (Feb 1, 2006) and subtract 1
from 28 (or 29, depending upon the year), take the result, and divide it by
15.345. Assuming a 28-day month, that leaves 27 days / 15.345 =
1.7595307917888563049853372434018 pay periods, which is added to 4 =
5.759530791788856304985337243401 pay periods, not counting the end. Now you
do the same for the second DateTime (May 28, 2006). For this we simply
divide 28 by 15.345 = 1.8246985988921472792440534376018 pay periods, added
to the existing 5.759530791788856304985337243401 pay periods =
7.584229390681003584229390681002 total pay periods.
Hopefully, if your business rule is different, such as using 15 for the
first pay period, and the remaining days in the month for the second, you
should be able to apply the same basic business rules I've provided to
develop your own algorithm.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
Big thicks are made up of lots of little thins.
"David P. Donahue" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Can anyone think of an easy way to take two DateTime values and calculate,
> with double precision, how many semi-monthly (the first and the fifteenth)
> pay periods occur between them?
>
> I started manually writing a function for it, and it just got uglier and
> uglier. And I figure it's something that someone has probably figured out
> before. Any ideas?
>
>
> Regards,
> David P. Donahue
> (E-Mail Removed)
> http://www.cyber0ne.com