Avg. Rating 3.5

Problem

You need to determine the difference between two dates or times.

Solution

Use dateDiff() to return the interval between two dates and/or times. The dateDiff() function is used to return the interval between two dates or times. The function takes three parameters, the interval for the comparison, and the two dates you want to compare.

Detailed explanation

The following code compares the difference between two dates in years, and the difference between two times in minutes:

<cfoutput>
#dateDiff("yyyy", "12/31/1999", "12/31/2002",)#<br>
#dateDiff("n", "12:00:00", "15:00:00")#
</cfoutput>

This interval can be expressed in a variety of formats:

Datepart     Interval
s            Second
n            Minute
h            Hour
ww           Week
w            Weekday
d            Day
y            Day of year
m            Month
q            Quarter
yyyy         Year


dateDiff() returns the interval in the unit you specify by which the second date/time is greater than the first. If the first date is greater than the second, a negative number is returned.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes