CFLib.org – Common Function Library Project

Duration(dateObj1 , dateObj2 )

Last updated November 18, 2001
Download UDF

author

Chris Wigginton                                   Chris Wigginton

Version: 1 | Requires: ColdFusion 5 | Library: DateLib

 
Rated 2 time(s). Average Rating: 4.0

Description:
Adapted from original concept of Craig Girard's CF_SubtractDates located in the Developer Exchange. This function takes two dates and produces a structure containing the difference in Days, hours, and minutes

Return Values:
Returns a structure containing the keys Days, Hours, and Minutes with their associated values.

Example:

<cfset today = "{ts '2001-11-15 08:55:56'}">
<cfset endDate = "{ts '2001-12-01 05:00:56'}">
<cfset timeDiff = Duration(today, endDate)>
<cfoutput>
#today#<br>
#endDate#<br>
#timeDiff.days# Day(s) #timeDiff.hours# Hour(s) #timediff.minutes# Minute(s)<p></p>
</cfoutput>

Parameters:

Name Description Required
dateObj1 CF Date Object to compare Yes
dateObj2 CF Date Object to compare Yes

Full UDF Source:

<cfscript>
/**
* Duration(dateObj1, dateObj2)
Takes two date objects and returns a structure containing the duration of days, hours, and minutes.
*
* @param dateObj1      CF Date Object to compare
* @param dateObj2      CF Date Object to compare
* @return Returns a structure containing the keys Days, Hours, and Minutes with their associated values.
* @author Chris Wigginton (cwigginton@macromedia.com)
* @version 1.0, November 18, 2001
*/

function Duration(dateObj1, dateObj2)
{
    var dateStorage = dateObj2;
    var DayHours = 0;
    var DayMinutes = 0;
    var HourMinutes = 0;
    var timeStruct = structNew();
    
    if (DateCompare(dateObj1, dateObj2) IS 1)
    {
            dateObj2 = dateObj1;
            dateObj1 = dateStorage;
    }
    
    timeStruct.days = DateDiff("d",dateObj1,dateObj2);
    DayHours = timeStruct.days * 24;
    timeStruct.hours = DateDiff("h",dateObj1,dateObj2);
    timeStruct.hours = timeStruct.hours - DayHours;

    DayMinutes = timeStruct.days * 1440;
    HourMinutes = timeStruct.hours * 60;
    timeStruct.minutes = DateDiff("n",dateObj1,dateObj2);
    timeStruct.minutes = timeStruct.minutes - (DayMinutes + HourMinutes);
    
    return timeStruct;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
2 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson