Question

I have starting date and number of months. I need to create specific number of monthly periods for example:

var startingDate = new DateTime(2010,1,15);
var months = 3;
for (int i = 0; i < months; i++)
 {
 Console.WriteLine("{0} from {1} to {2}", i + 1, startingDate.AddMonths(i), 
startingDate.AddMonths(i + 1).AddDays(-1));
}
OUTPUT:
1 from 2010-1-15 to 2010-2-14
2 from 2010-2-15 to 2010-3-14
3 from 2010-3-15 to 2010-4-14

In this case code is simple and it works. However when startDate is DateTime(2010,1,31) result is:

OUTPUT:
1 from 2010-1-31 to 2010-2-27
2 from 2010-2-28 to 2010-3-30
3 from 2010-3-31 to 2010-4-29

Are these periods correct?

Was it helpful?

Solution

The periods do look funky but they are correct.

If your periods are starting on the last day of the month they will end on the second to last day of the next month.

OTHER TIPS

You get to choose if they are right or wrong. Here is what I mean : If you start your period on the 15th of January, are you starting 14 days after the first day of the month, or are you starting 16 days before the last?

It can get even more tricky. If the 15th is a Tuesday, is your period defined as starting the 3rd Tuesday of a given month?

There is a lot of literature about this in the financial community, since the Day Count Conventions, the Business Days, Rolling Conventions, etc. can make a lot of difference in the pricing of a financial product, and in the cash flows associated with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top