Date Addition Using CreateTimeSpan() Creates A Numeric Date

Posted May 19, 2009 at 8:53 AM by Ben Nadel

Tags: ColdFusion

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()!


You Might Also Be Interested In:



Reader Comments

May 19, 2009 at 10:20 AM // reply »
20 Comments

Why not just use dateadd('n',5'now())? to adjust your date. Then you don't have to worry about numeric dates?


May 19, 2009 at 10:25 AM // reply »
11,238 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.


May 19, 2009 at 10:47 AM // reply »
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.


May 19, 2009 at 10:50 AM // reply »
11,238 Comments

@Akbarsait,

I find it solves my problems :)


May 19, 2009 at 11:09 AM // reply »
20 Comments

@Ben, I guess using createtimespan makes more sense if you want to add something like 1 hour and 5 minutes.


May 19, 2009 at 11:10 AM // reply »
11,238 Comments

@Anthony,

Yeah, I really like the way it easily breaks out all the time fields. Very easy to work with.


May 19, 2009 at 10:27 PM // reply »
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.


May 20, 2009 at 4:07 AM // reply »
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!


May 20, 2009 at 4:08 AM // reply »
20 Comments

my head. sorry. it's early...


May 20, 2009 at 10:45 PM // reply »
11,238 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().


Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools