CFLib.org – Common Function Library Project

isODBCDate(str)

Last updated July 24, 2012

author

Paul Klinkenberg

Version: 1 | Requires: CF9 | Library: CFMLLib

Description:
The function checks if a string is an ODBC formatted date, time, or timestamp; returns a boolean. Checks for validity of the time and date, between the year 1000 and 3999.

Return Values:
True if the string is a correctly-formatted ODBC date string, otherwise false

Example:

<cfoutput><pre>
	<cfset s = "{d '2012-02-28'}">
	isODBCDate("#s#") = #isODBCDate(s)#<br />
	<cfset s = "{d '2012-02-31'}">
	isODBCDate("#s#") = #isODBCDate(s)#<br />
	<cfset s = "{d '2012-02-31'}">
	isODBCDate("#s#") = #isODBCDate(s)#<br />
	<cfset s = "{d '2012-19-39'}">
	isODBCDate("#s#") = #isODBCDate(s)#<br />
</pre></cfoutput>

Parameters:

Name Description Required
str The string to validate Yes

Full UDF Source:

<!---
 Checks if a string is an ODBC formatted date, time, or timestamp
 version 0.1 by Paul Klinkenberg
 version 1.0 by Adam Cameron - adding date validation so it will fail invalid dates such as Feb 31.
 
 @param str 	 The string to validate (Required)
 @return True if the string is a correctly-formatted ODBC date string, otherwise false 
 @author Paul Klinkenberg (pauL@ongevraagdadvies.nl) 
 @version 1, July 24, 2012 
--->
<cffunction name="isODBCDate" access="public" returntype="boolean" output="false">
	<cfargument name="str" required="yes" type="string">
	<cfscript>
		// test the format
		if (!(len(str) gt 10 and refindNoCase("^\{(d|t|ts) \'([1-3][0-9]{3}\-[0-1][0-9]\-[0-3][0-9] ?)?([0-2][0-9]:[0-5][0-9]:[0-5][0-9])?\'\}$", str))){
			return false;
		}
		// test that it's actually a valid date (ie: not 31 Feb, etc)
		try {
			parseDateTime(str);
			return true;
		}
		catch (any e){
			return false;
		}
	</cfscript>
</cffunction>
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