Last night one of my clients asked a question that I thought would be great for a CF 101 posting. The question was - if I have an event on a certain date, how do I show the number of days till that event? Luckily ColdFusion makes this rather simple. You can use the dateDiff() function to return the number of days between today and the date of the event. Let's look at a simple example:

<cfoutput> There are #dateDiff("d", now(), createDate(2005, 11, 22))# days till the XBox 360 is released! </cfoutput>

This code sample returns the number of days between today (using the now function) and November 22. Nice and simple, right? There is one small problem, however. On the actual day of November 22nd, this is what the user will see:

There are 0 days till the XBox 360 is released!

So - what can you do? Well certainly you can just logon to your web site that morning and change the code, but I know you would much rather be actually playing the XBox 360 than writing code. Luckily we can handle it easily enough with a bit more code:

<cfset xbox360Date = createDate(2005, 11, 22)> <cfset today = createDate(year(now()), month(now()), day(now()))>

<cfset daysTill = dateDiff("d", today, xbox360Date)>

<cfif daysTill gte 1>

<cfif daysTill is 1> There is 1 day <cfelse> <cfoutput>The are #daysTill# days</cfoutput> </cfif> till the XBox 360 is released!

<cfelseif daysTill is 0>

The XBox 360 was released today! Go buy it!

<cfelse>

The XBox 360 was released already. I hope you got yours!

</cfif>

So a few things changed here. Let's take it step by step. On the first line, I create a variable to store the event date. On the second line I create a variable for today. Now you may wonder - why did I use createDate instead of just now()? Now returns the precise time. Let's pretend today is November 21, 12 PM. At that time, the XBox will be released in one day, but to be more precise, it is actually less than a day. If we use dateDiff between now() and the event date, the value will be 0, since there isn't a full day between them. Using createDate as I have done using a time of midnight. Therefore on November 21st, we will properly get a value of 1.

So once I have my daysTill variable, I simply use a cfif block to determine what message to print. Notice in the "1" block, I get a bit more precise with my message ("There is" compared to "There are").