CFLib.org – Common Function Library Project

isTime(time[, formatOption])

Last updated March 4, 2010

author

Mosh Teitelbaum

Version: 0 | Requires: CF6 | Library: DateLib

Description:
Validates if a string is a valid time in 12- or 24-hour format. Optionally, can require time be in a specific format.

Return Values:
Returns a boolean.

Example:

isTime("1:23 AM")<br />
isTime("1:23:45 pm", "12")<br />
isTime("23:00")<br />
isTime("23:00:00", "24")

Parameters:

Name Description Required
time time string to check format Yes
formatOption argument to check if 12 or 24 hr format required No

Full UDF Source:

/**
 * Validates if a string is a valid time in 12- or 24-hour format
 * 
 * @param time 	 time string to check format (Required)
 * @param formatOption 	 argument to check if 12 or 24 hr format required (Optional)
 * @return Returns a boolean. 
 * @author Mosh Teitelbaum (mosh.teitelbaum@evoch.com) 
 * @version 0, March 4, 2010 
 */
function isTime(time) {
	var found=0;
	if ( (arrayLen(arguments) eq 2) AND (arguments[2] is "12")) {
		found=reFindNoCase("^((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [ap]m))$", arguments.time);
	} else if ( (arrayLen(Arguments) eq 2) AND (Arguments[2] is "24")) {
		found=reFindNoCase("^([01]\d|2[0-3])(:[0-5]\d){0,2}$", arguments.time);
	} else {
		found=reFindNoCase("^([01]\d|2[0-3])(:[0-5]\d){0,2}$|^((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [ap]m))$", arguments.time);
	}
	if (found GT 0)
		return true;
	else
		return false;
}
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