I’ve been published! The (vast) majority of my web development is done in Cold Fusion. Recent versions allow for user-defined functions, which essentially allow you to run commonly used blocks of code using a shortcut. (Not a great definition, but that’s more or less what it does)
My function takes two dates and returns a string with redundant date formatting removed (”August 3 - 11, 2003″, “December 23 - August 11, 2003″). I’ll often use this to display dates coming out of an events calendar - by letting the UDF do the work, I only have to pass the start and end date of the event, and it’s displayed correctly.
I originally submitted the UDF, oh, maybe 2 months ago. I had assumed that it wasn’t going to go up on the cflib.org, but last night I got a flurry of e-mails from Raymond Camden about it. There was a bug in my code, which he had already modified. We went through a revision or two, and now it’s up.
You can view and download the DateRangeFormat function from the Common Function Library Project.
/**
* Format a range of dates ("August 3 - 11, 2003").
*
* @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, January 6, 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) GT 2 AND startDate NEQ endDate) {
DateRangeFormat = startFormat & ” - ” & endFormat;
} else {
DateRangeFormat = dateFormat(startDate,applyformat);
}
return trim(DateRangeFormat);
}
Comments
No comments yet