Not yet rated

Problem

You need to determine whether a string or numeric value is a valid date object.

Solution

Use isDate() or LSIsDate() for strings, or isNumericDate() for numeric values.

Detailed explanation

The isDate() function returns True if the specified string can be converted to a valid date/time object. Be sure to enclose literal dates in quotation marks.

<cfoutput>
<cfset x="12-31-2002">

#isDate(x)#<BR>
#isDate('12/31/2002')#<BR>
#isDate('Dec 31, 2002')#<BR>
#isDate('13/31/2002')#<BR>
#isDate('19:00')#<BR>
#isDate('7pm')#
</cfoutput>

You should be aware that the isDate() function only works with dates formatted for the U.S. locale. If you have a date formatted using a different locale, you should use the LSIsDate() function. It returns True if the specified date can be converted to a date/time object in the current locale or False if not.

If you need to determine whether a numeric value (real number) is a valid date/time object, you can use the isNumericDate() function. Like isDate(), it returns True if the value you pass to it can be converted to a date/time object.

<cfoutput>
#isNumericDate(37621.79167)#<BR>
#isNumericDate(-1)#
</cfoutput>

Because isNumericDate() operates on numeric values, it can be used in any locale.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes