CFLib.org – Common Function Library Project

DateRangeFormat([startDate] [, endDate] [, format])

Last updated June 8, 2004

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

 
Rated 3 time(s). Average Rating: 3.7

Description:
DateRangeFormat() accepts three arguments: a start date, an end date, and the format mask to be used. Returns a string with redundant date formatting removed ("August 3 - 11, 2003", "December 23 - August 11, 2003").

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>
<!--- same date --->
#DateRangeFormat(CreateDate(2003,8,3),CreateDate(2003,8,3),'long')#<br>
<!--- same month --->
#DateRangeFormat(CreateDate(2003,8,3),CreateDate(2003,8,11),'long')#<br>
<!--- same year --->
#DateRangeFormat(CreateDate(2003,6,3),CreateDate(2003,8,11),'long')#<br>
<!--- all different  --->
#DateRangeFormat(CreateDate(2002,6,3),CreateDate(2003,8,11),'long')#<br>

<!--- same date --->
#DateRangeFormat(CreateDate(2003,8,3),CreateDate(2003,8,3),'short')#<br>
<!--- same month --->
#DateRangeFormat(CreateDate(2003,8,3),CreateDate(2003,8,11),'short')#<br>
<!--- same year --->
#DateRangeFormat(CreateDate(2003,6,3),CreateDate(2003,8,11),'short')#<br>
<!--- all different  --->
#DateRangeFormat(CreateDate(2002,6,3),CreateDate(2003,8,11),'short')#<br>
</cfoutput>

Parameters:

Name Description Required
startDate Initial date. Defaults to now. No
endDate Ending date. Defaults to now. No
format Either "long" or "short". Defaults to long. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Format a range of dates (&quot;August 3 - 11, 2003&quot;).
 * Small bug in last statement was losing end date. RKC
 * 
 * @param startDate      Initial date. Defaults to now. (Optional)
 * @param endDate      Ending date. Defaults to now. (Optional)
 * @param format      Either "long" or "short". Defaults to long. (Optional)
 * @return Returns a string. 
 * @author Bryan Buchs (bbuchs@mac.com) 
 * @version 1, June 8, 2004 
 */

function DateRangeFormat() {
    var format = "long";
    var longformat = "mmmm d, yyyy";
    var shortformat = "m/d/yy";
    var applyformat = longformat;
    var startDate = now();
    var endDate = now();
    var startFormat = DateFormat(startDate,format);
    var endFormat = DateFormat(endDate,format);
    var DateRangeFormat = startFormat;
    
    if (arrayLen(arguments) GTE 1) { startDate = arguments[1]; }
    if (arrayLen(arguments) GTE 2) { endDate = arguments[2]; }
    if (arrayLen(arguments) GTE 3) { format = arguments[3]; }
    
    if(format is not "long" and format is not "short") format = "long";
    if(format is not "long") applyformat = shortformat;
    
    //case one, same month and year
    if(year(startDate) is year(endDate) and month(startDate) is month(endDate)) {
        startFormat = dateFormat(startDate,ReplaceNoCase(applyformat,"y","","All"));
        if(format is "long"{
            endFormat = dateFormat(endDate,ReplaceNoCase(applyformat,"m","","All"));
        } else {
            endFormat = dateFormat(endDate,applyformat);
        }
    } else if(year(startDate) is year(endDate)) {
    //case two, same year
        startFormat = DateFormat(startDate,ReplaceNoCase(applyformat,"y","","All"));
        endFormat = DateFormat(endDate,applyformat);
    } else {
    //case three, different year and month, dont change anything
        startFormat = DateFormat(startDate,applyformat);
        endFormat = DateFormat(endDate,applyformat);
    }

    if (right(trim(startFormat),1) EQ "," or right(trim(startFormat),1) EQ "/"{ 
        startFormat = trim(RemoveChars(startFormat,len(trim(startFormat)), 1)); 
    }

    if (arrayLen(arguments) GTE 2 AND startDate NEQ endDate) {
        DateRangeFormat = startFormat & " - " & endFormat;
    } else {
        DateRangeFormat = dateFormat(startDate,applyformat);
    }
    
    return trim(DateRangeFormat);
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

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

Ray Ford Ray Ford added
timeZoneNow
28 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