CFLib.org – Common Function Library Project

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

Last updated June 8, 2004

author

Bryan Buchs

Version: 1 | Requires: CF6 | Library: DateLib

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:

<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:

/**
 * 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);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson