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!
Tags: calculation, month, year
Sign up now to get your free ebook of "How to self-publish an online magazine". Your email is kept confidential, and is used only to send information about the magazine.



Hi! I write about maths and programming and other topics of esoteric interest. I'm also the editor of the online magazine Singularity, and you can get the latest issue at the top (it's free!).
