Friday Puzzler - Gold baby, pure gold...

This post is more than 2 years old.

Earlier today Andy Sandefer sent me a message saying he that he thought my blog had a 'golden birthday' today. He saw 2009 ColdFusion blog entries (i.e., 2009 entries in the CF category, not total) and the year was 2009. A golden birthday is defined as the birthday when your age matches the day of the month for your birthday. So for example, my birthday, April 8 (hey, that's soon!), means that I had my golden birthday when I was 8 years old in 1981.

Your Friday Puzzler is to write a UDF that will generate the Golden Birthday date for a given date of birth. The "winner" (not that I have anything to give away ;) will have their UDF added to CFLib.org. (In other words, I'll post it/approve it for you.)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Joshua Siok posted on 3/20/2009 at 7:50 PM

<cfcomponent>
<cffunction name="GoldenBirthday" access="public" returntype="date">
<cfargument name="BirthDate" type="date" required="yes">

<cfset DayOfMonth = Day(BirthDate)>
<cfset GoldenBirthday = DateAdd("yyyy", DayOfMonth,BirthDate)>

<cfreturn GoldenBirthday>
</cffunction>
</cfcomponent>

Comment 2 by Joshua Siok posted on 3/20/2009 at 7:59 PM

Can be simplified to:

<cfcomponent>
<cffunction name="GoldenBirthday" access="public" returntype="date">
<cfargument name="BirthDate" type="date" required="yes">

<cfset GoldenBirthday = DateAdd("yyyy", Day(BirthDate),BirthDate)>

<cfreturn GoldenBirthday>
</cffunction>
</cfcomponent>

Comment 3 by Michael De Jonghe posted on 3/20/2009 at 8:09 PM

<cfscript>

function goldenbirthday(dob) {

var daydob = day(dob);
var goldenday = dateadd("yyyy",daydob,dob);

return goldenday;

}
</cfscript>

<cfoutput>#goldenbirthday(dob)#</cfoutput>

Comment 4 by John Gag posted on 3/20/2009 at 8:54 PM

I have a better question... How can a wonderful friday be ruined so quickly???

Comment 5 by Raymond Camden posted on 3/20/2009 at 8:56 PM

My question ruined your Friday?? ;) Have a beer on me (or with me... in 4 hours or so )

Comment 6 by Rick O posted on 3/20/2009 at 9:46 PM

Just to be different, since everyone else was jumping on the dateAdd wagon.

<cffunction name="goldenBirthday">
<cfargument name="dob" type="date">
<cfreturn createDate(year(arguments.dob) + day(arguments.dob), month(arguments.dob), day(arguments.dob))>
</cffunction>

Comment 7 by Andy Sandefer posted on 3/20/2009 at 10:03 PM

You gotta love the shameless self promo of the link to the wish list! Keep your eyes peeled Ray, I'll probably send you something around then (04/08). You solved plenty of problems for me in 2008 and you probably will in 2009 so consider it a down payment on keeping my sanity!

Comment 8 by Raymond Camden posted on 3/20/2009 at 10:05 PM

Me? Pimp my wishlist?? Never! I'm offended you even suggested that!

Comment 9 by Steve Withington posted on 3/20/2009 at 11:20 PM

Thought I would submit a "fancier" version:

<cfcomponent displayname="Golden Birthday (Fancy)"
output="false"
hint="Send me a birthday, I'll give you all kinds of interesting info including when your Golden Birthday is (or was).">

<cffunction name="goldenBirthday"
displayname="Check Birthday (Returns Struct)"
access="public"
returntype="struct"
output="no">

<cfargument name="birthDate"
type="date"
required="yes" />

<cfset var local = structNew() />
<cfset local.isGoldenToday = false />
<cfset local.yourBirthday = dateformat(arguments.birthDate, 'mm/dd/yyyy') />
<cfset local.todaysDate = dateformat(now(), 'mm/dd/yyyy') />
<cfset local.todaysDay = day(local.todaysDate) />
<cfset local.yourDayToMatch = day(local.yourBirthday) />
<cfset local.yourAgeYears = dateDiff("yyyy", local.yourBirthday, local.todaysDate) />

<cfset local.yourGoldenBirthday = dateformat(dateAdd("yyyy", local.yourDayToMatch, local.yourBirthday), 'mm/dd/yyyy') />

<cfswitch expression="#dateCompare(local.yourGoldenBirthday, local.todaysDate, 'd')#">

<!--- already happened --->
<cfcase value="-1">
<cfset local.message = "Sorry, your Golden Birthday already happened on " & local.yourGoldenBirthday & ". But that's ok, because you're still golden in _my_ eyes." />
</cfcase>

<!--- is right now! --->
<cfcase value="0">
<cfset local.message = "Your Golden Birthday is TODAY! Congratulations!!! Can I have some cake and ice cream?" />
<cfset local.isGoldenToday = true />
</cfcase>

<!--- is coming up --->
<cfcase value="1">
<cfset local.message = "Your Golden Birthday is coming up! In fact, it will occur on " & local.yourGoldenBirthday & ". Am I invited to the party?" />
</cfcase>

</cfswitch>

<cfreturn local />

</cffunction>

</cfcomponent>

Comment 10 by Steve Withington posted on 3/20/2009 at 11:36 PM

Just for fun I threw this form together for testing:

<style type="text/css">
body {
background-color: #FFFFFF;
margin: 0;
padding: 0;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #666666;
}
.wrapper {
margin: 20px;
padding: 20px;
border: 1px dashed #666666;
}
.wrapper h1 {
font-size: 16px;
color: #000000;
}
.wrapper h2 {
font-size: 14px;
color: #FF0000;
}
select, input, textarea {
font-family: Arial, Helvetica, sans-serif;
font-size: 100%;
}
</style>

<cfparam name="isSubmitted" default="false" />
<cfparam name="form.bDate" default="04/08/1973" />
<div class="wrapper">

<cfform action="#cgi.script_name#"
method="post"
enctype="application/x-www-form-urlencoded"
name="form1"
id="form1">

<strong>Your Birthday</strong> (mm/dd/yyyy):<br />
<cfinput type="datefield" name="bDate" id="bDate" value="#form.bDate#" mask="mm/dd/yyyy" /><br /><br />
<cfinput name="isSubmitted" type="hidden" id="isSubmitted" value="true" />
<cfinput type="submit" name="btnSubmit" label="Submit" value="Submit" validate="submitonce" />

</cfform>

</div>

<cfif isSubmitted>

<div class="wrapper">

<h1>goldenBirthdayFancy</h1>

<cfinvoke
component="goldenBirthday"
method="goldenBirthday"
returnvariable="goldenBirthdayInfo">
<cfinvokeargument name="birthDate" value="#form.bDate#" />
</cfinvoke>

<cfdump var="#goldenBirthdayInfo#" label="goldenBirthdayInfo" />

</div>
</cfif>

Comment 11 by Andy Sandefer posted on 3/20/2009 at 11:38 PM

@Steve
I didn't try to run this but you've won a lifetime's supply of pure cheese because of the dialog in your switcher where you said, "Sorry, your Golden Birthday already happened on <ENTER DATE HERE>. But that's OK, because you're still golden in my eyes."
So Steve, if you're not working for Hallmark yet then what in the hell are you waiting for?
LOL

Comment 12 by Steve Withington posted on 3/20/2009 at 11:52 PM

@Andy,

Thanks ... yeah, I was a little bored this afternoon. :)

Comment 13 by Torsten Lofhelm posted on 3/23/2009 at 4:32 AM

I like the idea but it won't work for Feb 29th, people would never have a "Golden Birthday"

Comment 14 by Raymond Camden posted on 3/23/2009 at 5:17 AM

Those people don't count.

Comment 15 by Raymond Camden posted on 4/8/2009 at 6:10 AM

Joshua, I'm using a slightly modified version of your UDF @ CFLib. Can you shoot me an email so I have your proper address?

Comment 16 by Raymond Camden posted on 4/8/2009 at 6:19 AM

I found it. Posting your UDF now. Let me know if you have any problems with it.