Utility Function: IsTime()
- January 7, 2009 5:30 AM
- Utility Function
- Comments (0)
My company often splits up date and time entries on forms, and then concatenates them back together on the back end. This is fine, except the time inputs are prone to errors, as IsDate() seems to be the only built in check to see if a string is a time, and it matches on things that are not purely times. To get around this, I create a new check: IsTime().
<cffunction name="IsTime" access="public" output="false" returntype="boolean">
<cfargument name="time" type="string" required="true" />
<cfreturn ArrayLen(REMatchNoCase("^((0?\d|1[012]):[0-5]\d ?(am|pm)|[0-2]?\d:[0-5]\d)$", arguments.time)) gt 0 />
</cffunction>
<cfargument name="time" type="string" required="true" />
<cfreturn ArrayLen(REMatchNoCase("^((0?\d|1[012]):[0-5]\d ?(am|pm)|[0-2]?\d:[0-5]\d)$", arguments.time)) gt 0 />
</cffunction>
Tests
String | IsDate()? | IsTime()? |
---|---|---|
3:45 am 1/1/2008 | YES | NO |
12:65 am | NO | NO |
47:00 | NO | NO |
09:34 PM | YES | YES |
1/1/2008 3:45 am | YES | NO |
3:45am | YES | YES |
3:45 am | YES | YES |
09 13:45 | YES | NO |
[Empty String] | NO | NO |
14:05 | YES | YES |
13:00 pm | YES | NO |
1/1/2008 13:45 | YES | NO |
4:45 | YES | YES |
12:45 am | YES | YES |