CFLib.org – Common Function Library Project

isTime(time [, formatOption])

Last updated March 4, 2010

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

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
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:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson