2006 Simple ColdFusion Date Difference Calculations

DateDiff() is usually used to perform date difference calculations. But for simple difference calculations you can subtract dates from each other, like this:

1<cfset mydate=CreateDate(2006,8,17)>

2<cfoutput>#Now()-mydate# days since #DateFormat(mydate)#</cfoutput>

2<cfoutput>#Now()-mydate# days since #DateFormat(mydate)#</cfoutput>

The returned number will likely not be an integer, and will contain date fractions too, so you may want to use Int() to round the number to just the integer portion:

1<cfset mydate=CreateDate(2006,8,17)>

2<cfoutput>#Int(Now()-mydate)#</cfoutput>

2<cfoutput>#Int(Now()-mydate)#</cfoutput>

Along those same lines, people can simply subtract days from a date. Since integers (in many cases) are a valid form of fractional date as a whole as well as time spans, (ie. 1.0 = 1 day, 2.0 = 2 days) the math should work out nicely:

(Now() - 1) === > Yesterday

(Now() + 1) === > Tomorrow

So then, doing date calculations in this manner always defaults to days as the unit, right? At least that's what it appears to be in the example.

Doug, correct.

--- Ben

Doug Hughes also found a nifty way to loop over dates. http://doughughes.net/index.cfm?event=viewEntry&am...