Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes and Ezra Parker and Dan Wilson and John Mason and Jason Dean and Luis Majano and Mark Mandel and Brian Kotek and Wil Genovese and Rob Brooks-Bilson and Andy Matthews and Simeon Bateman and Ray Camden and Chris Rockett and Joe Bernard and Dan Skaggs and Byron Raines and Barney Boisvert and Simon Free and Steve 'Cutter' Blades and Seth Bienek and Katie Bienek

Quick Note On IsDate() vs. IsNumericDate()

Posted by Ben Nadel
Tags: ColdFusion

After my post yesterday on date math, someone sent me a question about IsDate() vs. IsNumericDate(). I briefly touch on the use of IsNumericDate() to determine if you have a numeric date, but I was not clear on exactly how it works. IsDate() takes a date/time object. IsNumericDate() takes a number. There are some caveats here. Since IsNumericDate() requires a number, if you send it a date/time object, ColdFusion will automatically convert the date/time object to a number, therefore allowing it to work. IsDate(), on the other hand, takes a date/time object and will NOT be true if passed a number, even if that number represents a date.

Take a look at this example:

  • <!--- Get date/time object. --->
  • <cfset dtNow = Now() />
  •  
  • <!--- Get NUMERIC date value. --->
  • <cfset flNow = (dtNow * 1) />
  •  
  • <!--- Output the date and test methods. --->
  • #DateFormat( dtNow )# - #IsDate( dtNow )# - #IsNumericDate( dtNow )#
  •  
  • <!--- Output the numeric date and test methods. --->
  • #DateFormat( flNow )# - #IsDate( flNow )# - #IsNumericDate( flNow )#

In that second line, just notice that I am multiplying the date by 1. This forces ColdFusion to convert the date/time object to a number. The code produces this output:

30-Aug-06 - YES - YES

30-Aug-06 - NO - YES

As you can see, a standard date/time object will return true for BOTH IsDate() and IsNumericDate(). A numeric date will return true for IsNumericDate(), but FALSE for IsDate(). Sorry if I was unclear about this before. As a safe guard, if you are working on a page where you might be working with numeric dates, I would recommend always using IsNumericDate() as it will give you the most useful responses.




Reader Comments

@Michael,

isDate() will work with a string - but, it won't work with a string that represents a number... is what I think they are saying. So, for example, this will return false:

isDate( "123" )

... but this will return true:

isDate( "jun 1, 2010" )

... both of which are strings. Of course, I might be misunderstanding what the docs are saying.

Reply to this Comment

Post A Comment

?
You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.