CreateTimeSpan() is special :)


Recently I was looking to store a ‘timespan’ in a config or ‘ini’ file. To store a truly configurable timespan I’d have to use a list (ie; cacheTime=0,0,2,0) and then programmatically split it up for use in the CreateTimeSpan function.

The ColdFusion docs say CreateTimeSpan returns a ’special’ date-time object. Experimenting with CreateTimeSpan, you can see it just returns numbers. These numbers when treated as a timespan or date have special meaning. But on their own they’re just numbers. So if I really wanted to, I could store these numbers in my config file. Yay.

[viewcode] src=”http://code.mrbuzzy.biz/blog/functions/createtimespan.cfm?sauce” geshi=cfm showsyntax=no lines=1-8[/viewcode]

So what is a timespan? It’s a length of time (in days) represented as a decimal fraction. ie; 0.25 is a timespan of 6 hours.

CreateTimeSpan is simple (in a good way). You could even roll your own ‘MyCreateTimeSpan’;

[viewcode] src=”http://code.mrbuzzy.biz/blog/udf/mycreatetimespan.cfm?sauce” geshi=cfm showsyntax=no lines=1-12[/viewcode]

But is a timespan also a date? Yes, by default a timespan is also the number of days since December 29th 1899. ie; 0.25 is ‘6am on Dec. 30th 1899′.
Note: the timespan value does not contain a date as such. It’s just a number - a ‘numeric date’.

From the DateFormat documentation;
When passing date/time value as a string, enclose it in quotation marks. Otherwise, it is interpreted as a number representation of a date/time object.

Cool, so stuff like this is possible;
[viewcode] src=”http://code.mrbuzzy.biz/blog/functions/createtimespan.cfm?sauce” geshi=cfm showsyntax=no lines=9-11[/viewcode]
So how can we create one of these numeric dates? Do we really want to? Not unless you’re a walking calendar who likes to write confusing code.
[viewcode] src=”http://code.mrbuzzy.biz/blog/functions/createtimespan.cfm?sauce” geshi=cfm showsyntax=no lines=12-13[/viewcode]
Finally (it’s getting late), not all ColdFusion tags/functions can handle the numeric date. I guess that’s why we have IsNumericDate()
[viewcode] src=”http://code.mrbuzzy.biz/blog/functions/createtimespan.cfm?sauce” geshi=cfm showsyntax=no lines=14-19[/viewcode]

Thanks for reading. Enlightened or stricken with boredom?

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts
Some people collect Star Wars figurines
Album Of The Month - Sep 07

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!