29 February ColdFusion bug

Written on 9 March 2012, 10:57pm

Tagged with: , ,

This is just to log a funny bug I encountered a few days ago, on the 29th of February 2012. There was a piece of code that was comparing if a certain date is older than a year or not.
The algorithm to compare the date was creating the ‘one-year-ago date’ by substracting one unit from the current year. Something like:

<cfset aYearAgo = CreateDate(year-1,month,day)>

Obviously, this algorithm was failing on the 29th of February on the leap years (like 2012).
The solution is quite simple: instead of substracting one unit from the year, substract 365 days from the current date, using Coldfusion’s dateAdd function:

<cfset aYearAgo = DateAdd('d', -365, Now())> 
Image: Smashing Magazine’s Desktop Wallpaper Calendar February 2012

Leave a response