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.
JimiJ13I T ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Ess KayEntrapenuerCommented:
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);

Open in new window







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);
}

Open in new window

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

Your issues matter to us.

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

Start your 7-day free trial
JimiJ13I 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.
käµfm³d 👽Commented:
Is this homework?
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

JimiJ13I T ConsultantAuthor Commented:
What do you mean by homework?
JimiJ13I T ConsultantAuthor Commented:
From the original source of solution these conditions work perfect for me:

http://stackoverflow.com/questions/8481517/how-many-days-to-add-for-semi-monthly

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

Thanks.
JimiJ13I T ConsultantAuthor Commented:
Your post provided me with a good lead in finding the right solution. Thanks
Gustav BrockCIOCommented:
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);
startPayDate = startPayDate.Day == 15 ? startPayDate.AddDays(1) : startPayDate.AddDays(1 - startPayDate.Day).AddMonths(1);
finalDate = startPayDate.AddMonths(installments / 2);
if (shift)
    {
  	    finalDate = finalDate.Day == 16 ? finalDate.AddMonths(1).AddDays(-15) : finalDate.AddDays(15);
    }
finalDate=finalDate.AddDays(-1);

Console.WriteLine(finalDate.ToString());

Open in new window

/gustav
JimiJ13I T ConsultantAuthor Commented:
Gustav,

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


Thanks.
Gustav BrockCIOCommented:
No problem.

/gustav
JimiJ13I T ConsultantAuthor Commented:
Gustav,

How can I work with BiMonthly or Monthly Installment?

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

Thanks.
JimiJ13I 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.
käµfm³d 👽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.
JimiJ13I T ConsultantAuthor Commented:
No!  I am upgrading my old MS Acesss VBA app to C# for a new client. Why?
Gustav BrockCIOCommented:
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.
	startPayDate = startPayDate.Day == 15 ? startPayDate.AddDays(1) : startPayDate.AddDays(1 - startPayDate.Day).AddMonths(1);
	// 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.
	        finalDate = finalDate.Day == 16 ? finalDate.AddMonths(1).AddDays(-15) : finalDate.AddDays(15);
	    }
	// Subtract one day to obtain as result either the 15th. or ultimo of the month.
	finalDate=finalDate.AddDays(-1);
	
	Console.WriteLine("Interval: " + interval.ToString());
	Console.WriteLine("Final date: " + finalDate.ToString());
}

Open in new window

/gustav
JimiJ13I T ConsultantAuthor Commented:
Thanks Gustav.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.