Skip to main content
Ben Nadel at CFUNITED 2008 (Washington, D.C.) with: Rupesh Kumar
Ben Nadel at CFUNITED 2008 (Washington, D.C.) with: Rupesh Kumar ( @rukumar )

Date Addition Using CreateTimeSpan() Creates A Numeric Date

By on
Tags:

This really isn't news; I am just writing this down to try and drill it into my head. I just spend 10 minutes debugging some old Caching code that uses a CacheUntil date/time stamp. When setting the CacheUntil value, I was using the CreateTimeSpan() to create a future date:

(Now() + CreateTimeSpan( 0, 0, 5, 0 ))

The cache never seemed to be expiring. As part of the logic, the caching controller was checking to see if the CacheUntil value was a date since non-date values would denote a non-expiring data store. It was using ColdFusion's IsDate() function to check the CacheUntil value; IsDate() is great, but it ignores numeric date/time representations. As such, the date I created using the CreateTimeSpan() math was being ignored:

<!--- Get a date that is 10 minutes in the future. --->
<cfset objFuture = (Now() + CreateTimeSpan( 0, 0, 10, 0 )) />

IsDate() :: #IsDate( objFuture )#<br />
IsNumericDate() :: #IsNumericDate( objFuture )#<br />

When we run the above code, we get the following output:

IsDate() :: NO
IsNumericDate() :: YES

As you can see, the date math produced a value that IsDate() did not recognize but IsNumericDate() did. I ran into this issue almost three years ago, but I still forget to use IsNumericDate() as my default date/time filter. Remember, remember, remember - just start using IsNumericDate()!

Want to use code from this post? Check out the license.

Reader Comments

15,663 Comments

@Anthony,

For some reason, I am simply not a huge fan of DateAdd(). It feels unnatural. Plus, if you are dealing with encapsulated logic or packaged products, I think its a good idea to make it as flexible as possible; if you require your calling code to provide standard date/time stamps only, it cuts out a lot of the logic that people will use to create date/time stamps.

9 Comments

I often use the IsDate() for my date validation but your point seems very valuable here and using IsNumericDate() over IsDate() will solve many problems like this I think.

6 Comments

I've always wondered why some date/times didn't show up as Yes using isDate. This will come in quite handy for me.

20 Comments

your comments to Anthony are insightful; I also would probably have just used dateAdd if I had come across the problem. But I'm not as experienced as you are. I think this is something programmers come across quite often...finding things that aren't new, but that they need to drill into their heads on a regular basis, because for whatever reason, they forget those certain things. Good post. It'll certainly help me remember and drill it into m head. Thanks!

15,663 Comments

@Anna,

Don't get me wrong - there's nothing wrong with DateAdd() at all. It's just that when you use it, you put more responsibility on the calling code to know the limitations of the implementation. Most of the time, its not an issue; but, anytime you are building something that involves non-standard times, it's probably a good idea to use IsNumericDate().

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel