CFLib.org – Common Function Library Project

listFromDateRange(date1, date2 [, thisDelimiter])

Last updated February 13, 2006

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

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
createPrimeNumbe...
14 day(s) ago

Ray Ford Ray Ford added
timeZoneNow
29 day(s) ago

Henry Ho Henry Ho added
queryExecute
a while ago

Rick Root Rick Root added
deleteDirectory
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 48 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 8 time(s)

Created by Raymond Camden / Design by Justin Johnson