How to get difference between two date time object in ColdFusion

DateDiff() - get difference between two date time object

DateDiff.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DateDiff function example: how to get difference between two date time object</title>
</head>

<body>
<h2 style="color:hotpink">DateDiff Function Example</h2>

<cfset MyDate="8-Dec-2006">
<cfset MyAnotherDate="10-Dec-2007">

<cfoutput>
<b>
MyDate: #MyDate#
<br />
MyAnotherDate: #MyAnotherDate#
<br /><br />
</b>
</cfoutput>
<table style="color:LightGreen; background:Green;" border="1" bordercolor="DarkGreen">
    <tr>
     <td style="font-weight:bold">DateDiff</td>
     <td style="font-weight:bold">DatePart</td>
     <td style="font-weight:bold">Output</td>
    </tr>
    <tr>
     <td>#DateDiff("yyyy",MyDate,MyAnotherDate)#</td>
     <td>Years</td>
     <td><cfoutput>#DateDiff("yyyy",MyDate,MyAnotherDate)#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateDiff("q",MyDate,MyAnotherDate)#</td>
     <td>Quarters</td>
     <td><cfoutput>#DateDiff("q",MyDate,MyAnotherDate)#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateDiff("m",MyDate,MyAnotherDate)#</td>
     <td>Months</td>
     <td><cfoutput>#DateDiff("m",MyDate,MyAnotherDate)#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateDiff("d",MyDate,MyAnotherDate)#</td>
     <td>Days</td>
     <td><cfoutput>#DateDiff("d",MyDate,MyAnotherDate)#</cfoutput></td>
    </tr>
</table>

</body>
</html>





More ColdFusion examples