CFLib.org – Common Function Library Project

listFromDateRange(date1, date2[, thisDelimiter])

Last updated February 13, 2006

author

Christopher Jordan

Version: 1 | Requires: CF5 | Library: StrLib

Description:
ListFromDateRange can be given a starting and ending date and will return an inclusive list of dates between those two dates. The resulting list will be comma delimited unless a different delimiter is specifically specified as the optional third argument.

Return Values:
Returns a list.

Example:

<cfset thisDate_1 = now()>
<cfset thisDate_2 = dateAdd("d",5,thisDate_1)>
<cfoutput>Result from listFromDateRange: #listFromDateRange(thisDate_1,thisDate_2)#<br></cfoutput>
<cfloop index="i" list="#listFromDateRange(thisDate_1,thisDate_2)#">
    <cfoutput>Date This Itteration: #i#<br></cfoutput>
</cfloop>

Parameters:

Name Description Required
date1 First date to use. Yes
date2 Second date to use. Yes
thisDelimiter List delimiter to use for result. Defaults to a comma. No

Full UDF Source:

/**
 * ListFromDateRange returns a list of dates given a starting and ending date.
 * 
 * @param date1      First date to use. (Required)
 * @param date2      Second date to use. (Required)
 * @param thisDelimiter      List delimiter to use for result. Defaults to a comma. (Optional)
 * @return Returns a list. 
 * @author Christopher Jordan (cjordan@placs.net) 
 * @version 1, February 13, 2006 
 */
function listFromDateRange (date1,date2) {
    var i                 = 0;
    var numberOfDays    = 0;
    var thisDate         = "";
    var theList            = "";
    var temp            = "";
    var thisDelimiter    = ",";

    if(arraylen(arguments) eq 3) thisDelimiter = trim(arguments[3]);
    
    if (date1 GT date2) {
        temp    = date1;
        date1    = date2;
        date2    = temp;
    }

    numberOfDays = dateDiff("d",date1,date2);
    
    for(i = 0; i lte NumberOfDays; i = i + 1){
        thisDate = dateAdd("d",i,date1);
        theList = listAppend(theList,thisDate,thisDelimiter);
    }
    
    return theList;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson