# Determine the last date

Dear Expert

Given the frequency, start paydate and number of installments, how to determine the last pay date?

Example:
1) Frequency - twice a month (15th and 30th)
2) Start Pay Date = 5/15/2015
3) No. of installment = 9
How to get the Final date?

Thanks.
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EntrapenuerCommented:
Try this

`````` // assuming first payment will be 1st of month, add month for every 2 payments
// num payments / 2 (int division, remainder is chucked)
// then add 15 days if this is even payment of the month
LastPaymentDate = FirstPaymentDate.AddMonths((NumberOfPayments - 1) / 2)
.AddDays((NumberOfPayments % 2) == 0 ? 15 : 0);
``````

So to illustrate, if we had:

``````DateTime LastPaymentDate, FirstPaymentDate = new DateTime(2011, 12, 5);

for(int numOfPayments=1; numOfPayments<=24; numOfPayments++)
{
LastPaymentDate = FirstPaymentDate.AddMonths((numOfPayments - 1) / 2)
.AddDays((numOfPayments % 2) == 0 ? 15 : 0);

Console.WriteLine(LastPaymentDate);
}
``````
This loop would give us:

12/5/2011 12:00:00 AM
12/20/2011 12:00:00 AM
1/5/2012 12:00:00 AM
// etc...
10/20/2012 12:00:00 AM
11/5/2012 12:00:00 AM
11/20/2012 12:00:00 AM

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

I T ConsultantAuthor Commented:
esskayb2d,

It works great if  FirstPaymentDate is not of the month.
If FirstPaymentDate = 6/30/2015 in 10 number of Payment (2X per month) it will give LastPaymentDate as 11/14/2015
If FirstPaymentDate = 2/28/2015 in 10 number of Payment (2X per month) it will give LastPaymentDate as 7/13/2015

How about if the payment is only once a month, every 15th or end of the month?

Thanks.
Commented:
Is this homework?
I T ConsultantAuthor Commented:
What do you mean by homework?
I T ConsultantAuthor Commented:
From the original source of solution these conditions work perfect for me:

case PaymentFrequency.BiMonthly:
LastPaymentDate = FirstPaymentDate.AddMonths((NumberOfPayments - 1) * 2); // 2 months
break;
case PaymentFrequency.Monthly:
break;

Thanks.
I T ConsultantAuthor Commented:
Your post provided me with a good lead in finding the right solution. Thanks
CIOCommented:
This will provide the desired result for any month:
``````DateTime startPayDate;
DateTime finalDate;
int installments;
bool shift;

startPayDate = new DateTime(2015, 5, 15);
installments = 9;

shift = (installments % 2 != 0);
if (shift)
{
}

Console.WriteLine(finalDate.ToString());
``````
/gustav
I T ConsultantAuthor Commented:
Gustav,

That must be great! I will try it. The only problem is that I already awarded the points.

Thanks.
CIOCommented:
No problem.

/gustav
I T ConsultantAuthor Commented:
Gustav,

How can I work with BiMonthly or Monthly Installment?

I'm not sure what 's the purpose of switch?

Thanks.
I T ConsultantAuthor Commented:
Gustav,

Currently your solution always resulted to additional one more installment on  BiMonthly basis.
Note: The FirstPayDate is already counted as one.

Thanks.
Commented:
What do you mean by homework?
I mean is this an assignment delegated to you by a professor or teacher with whom you are taking a school course.
I T ConsultantAuthor Commented:
No!  I am upgrading my old MS Acesss VBA app to C# for a new client. Why?
CIOCommented:
I've added a setting for fortnightly, monthly, or bimonthly installments:
``````enum Frequency
{
Fortnight = 1,
Monthly = 2,
Bimonthly = 4
};

void Main()
{
DateTime startPayDate;
DateTime finalDate;
int installments;
int interval;
bool shift;

// Set start date, count of installments, and interval.
startPayDate = new DateTime(2015, 5, 15);
installments = 4;
interval = (int)Frequency.Fortnight;

// Include start date as the first install.
installments--;
// Determine if an uneven count of installments in addition to the first shall be done.
shift = (installments * interval % 2 != 0);
// Move first date to one day after either the 15th. or ultimo of the month.
// Calculate final date for an even count of missing installments.
finalDate = startPayDate.AddMonths(installments * interval / 2);
if (shift)
{
// Add one installment for an uneven count of missing installments.
}
// Subtract one day to obtain as result either the 15th. or ultimo of the month.