[thelist] calling all regex studs
Jeff Howden
jeff at alphashop.net
Wed Aug 22 18:58:18 CDT 2001
hey,
got a cf user-defined function i'm writing that combines two dates into a
logical date pair string. i thought while i'm taking the time to write it i
might as well include the ability to set the mask of the output. thanks to
this thinking i now have to come up with a way of stripping portions of the
mask off so that i can combine the strings in the format most appropriate
for the combination. take the following examples:
start: January 4, 2001
end: January 6, 2001
pair: January 4 - 6, 2001
start: May 5, 2002
end: June 6, 2002
pair: May 5 - June 6, 2002
start: December 21, 2002
end: January 13, 2003
pair: December 21, 2002 - January 13, 2003
i'm assuming that the mask will in an american-style spelled format (ie, Jan
05, 2001, January 5, 2001, Jan 5, 01, etc.) and not in a euro-style spelled
format (5 January 2001, 05 Jan 01, 05 Jan 2001, etc.) or slashed format (ie,
1/5/2001, 01/05/2001, 1/5/01, etc.) as it would be fairly pointless to
output a slashed format as described above. i'll attempt to address the
euro-style spelled format at some point in the future, but first things
first.
so, noting that, here's what i've got so far (unfortunately some of the long
lines will wrap in this email. note the clean indents to restore it):
function DatePairFormat(startdate, enddate, mask)
{
var date_pair = '';
if(DateDiff('y', startdate, enddate))
{
if(DatePart('yyyy', enddate) - DatePart('yyyy', startdate))
date_pair = DateFormat(startdate, mask) & ' - ' &
DateFormat(enddate, mask);
else if(DatePart('M', enddate) - DatePart('M', startdate))
date_pair = DateFormat(startdate, REReplaceNoCase(mask, '[y*]', '',
'ALL')) & ' - ' & DateFormat(enddate, mask);
else
date_pair = DateFormat(startdate, REReplaceNoCase(mask, '[y*]', '',
'ALL')) & ' - ' & DateFormat(enddate, REReplaceNoCase(mask, '[m*]', '',
'ALL'));
}
else
date_string = DateFormat(startdate, mask);
return date_pair;
}
now, the problem i'm having with it the way it is, is when i call it like
this:
DatePairFormat(Apr 1 2001 12:00AM, Apr 15 2001 12:00AM, "mmmm d, yyyy")
i get this as the output:
April 1, - 15, 2001
obviously i need to strip the ", " (comma and trailing space).
i'm not terribly good with regular expressions so i don't know how to tell
it to remove an optional comma just before the year as well as an optional
space between the optional comma and the year.
i also welcome any other input on the function and ideas for making it more
useful/efficient/stable/etc.
thanks,
.jeff
http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/
More information about the thelist
mailing list