Blog

29Aug
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:

view plain print about
1<cfset mydate=CreateDate(2006,8,17)>
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:

view plain print about
1<cfset mydate=CreateDate(2006,8,17)>
2<cfoutput>#Int(Now()-mydate)#</cfoutput>

Comments (4)



  • Ben Nadel

    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

    #1Posted by Ben Nadel | Aug 29, 2006, 11:23 AM
  • Doug Boude

    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.

  • Ben Forta

    Doug, correct.

    --- Ben

    #3Posted by Ben Forta | Aug 29, 2006, 02:16 PM
  • Scott Stroz

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