Remember to change the year too

There were occasions when I was to increment or decrement some date value by one month. For example, “200806” to “200807”.

No, I don’t have any disaster-insider-programmer stories to share. Although there was this fellow who forgot to change the year which resulted in a financial fiasco…

Anyway, this is more like a reminder to myself. Take care when January and December are involved. Change the year when going from say “200812” to “200901”, and also “200801” to “200712”.

If you’re bouncing from one date to another by multiple months, you could try storing the year and the month into one variable holding total months. So “200806” becomes 2008 * 12 + 6 = 24102 months. Moving 15 months ahead, we have 24117 months, which is 2009 * 12 + 9. So it’s “200909” finally.

Of course, if your standard code libraries have an inbuilt function, just use that. For .NET, it’s

DateTime.Now.AddMonths(15).ToString("yyyyMM");

Here’s something to think about: Using the algorithm above, what year and month is 24120 months?

Have a fantastic year ahead!