So it’s once again February 29... that one time every four years when newspapers do their fluff pieces about people who only have a birthday every four years, and you find all kinds of obscure bugs in your code. I managed to save two examples from the last leap day that I thought would be best shared today.

The first is from Eric, who wrote "my client is an alcohol company, and I run their website. They have a members-only section of their site that requires that you to enter your birthdate. If you're less than 21 years old, they kick you out and won't let you onto the site until you come back on and lie about your date-of-birth."

int intDay  = (DateTime.Now.Day);
int intMon  = (DateTime.Now.Month);
int intYear = (DateTime.Now.Year - 21);
string LegalYear = (intMon + "/" + intDay + "/" + intYear);
	
//validate birthdate first
if(!(Convert.ToDateTime(this.birthdate.Text) <= Convert.ToDateTime(LegalYear)))
{
	this.messageLabel.Text = "Sorry, go away";
	return;
}

"Yes. Rather than just doing a DateTime.Today.AddYears(-21), instead the original author decided to build a new string by concatenating the month, day, and year-21 of this date. Which means that today, 2/29/2008, it tried to parse into a DateTime the string '2/29/1987', which throws an (uncaught, naturally) exception."

 

Next up, Daniel shares this story from the last Feburary 29:

"I'm in support for a financial software company, but I work closely with the developers here. We kept having the same issue come up this morning from several of our customers. After puzzling over it for a while, we get a note in our e-mail from development:

"Two clients have called in already with "type mismatch error" before it ever even shows them the parameter screen. This is going to happen on any client that does NOT use calendar years as fiscal years that runs Widget Report. Workaround: run the report tomorrow or set system date to tomorrow."

"Seems as the software forgot to see that there was a February 29th this year, because when the fiscal years cross calendar years, it creates the months in the fiscal year based on the current calendar year. Since the year was created in 2007, there was no February 29th to be had. Let's just hope that there's no Feb 29, 2009 that comes up.