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.)
Archived Comments
<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>
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>
<cfscript>
function goldenbirthday(dob) {
var daydob = day(dob);
var goldenday = dateadd("yyyy",daydob,dob);
return goldenday;
}
</cfscript>
<cfoutput>#goldenbirthday(dob)#</cfoutput>
I have a better question... How can a wonderful friday be ruined so quickly???
My question ruined your Friday?? ;) Have a beer on me (or with me... in 4 hours or so )
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>
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!
Me? Pimp my wishlist?? Never! I'm offended you even suggested that!
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>
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>
@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
@Andy,
Thanks ... yeah, I was a little bored this afternoon. :)
I like the idea but it won't work for Feb 29th, people would never have a "Golden Birthday"
Those people don't count.
Joshua, I'm using a slightly modified version of your UDF @ CFLib. Can you shoot me an email so I have your proper address?
I found it. Posting your UDF now. Let me know if you have any problems with it.