Date Objects vs Date Strings in ColdFusion

To use dates effectively in ColdFusion it is important to understand when you are dealing with dates represented internally as Strings and when you are dealing with dates represented internally as date Objects.

This is particularly important when dealing with non US dates formats.

So how do you know when you are using a date string or using a date object?

Some examples of dates stored within ColdFusion as strings

1) Dates submitted as form data

2) Dates constructed by joining strings together in an application. For example:

<cfset myDate = day & "/" & month & "/" & year>

3) Dates hardcoded in an application. For example:

<cfset myDate = "1/2/2007">

4) Dates read from text fields in a database

When you <cfoutput> a date string on the screen it looks identical to the original string:

So the code:

<cfset myDate = "23/10/2006">
<cfoutput>#myDate#</cfoutput>

simply displays:
23/10/2007
23/10/2006

Some examples of dates stored within ColdFusion as actual Date objects

1) Dates returned by most of the date functions are date objects. These include:
now()
createDate()
createDateTime()
parseDateTime()
lsParseDateTime()

2) Dates read from date or timestamp type fields in a database

When you display a date object on the screen it displays a representation of the date. For example, the code:

<cfset myDate = now()>
<cfoutput>#myDate#</cfoutput>

Displays something like:
{ts ’2006-10-23 20:17:35′}

Now you know you’re dealing with a real date object when you see something like this.

Remember that this is just a visual representation of the date. It is not really stored internally as a string starting with "{ts" inside ColdFusion.

How does ColdFusion work with dates?

Whenever you need to perform any kind of date operation, ColdFusion first ensures you are working with a real date object, and not a string.

If you have a date string, then ColdFusion will silently convert the string to date object and then perform the operation.

ColdFusion’s silent confusion with string to date conversions

OK, so if you are dealing with strings then ColdFusion silently converts them to dates:

Consider the following code:

<cfset d1 = "2/3/2007">
<cfset d2 = "13/3/2007">
<cfoutput>
#month(d1)#<br>
#month(d2)#<br>
</cfoutput>

The result here is:

2

3

So, for the first date, ColdFusion assumes the date is in US m/d/y format. For the second date, this assumption does not work (no such thing as month 13) so it next guesses that the date is in d/m/y format instead.

Always perform date parsing on your date strings

Whenever you need to work with dates always convert them to date objects before you do anything else.

This primarily affects code that deals with non US dates, so you should

1) Ensure you have set the correct locale using setLocale()

2) Use lsParseDateTime() on your date strings

So rather than having code such as:

<cfset d1 = "2/3/2007">
<cfset d2 = "13/3/2007">
<cfoutput>
#month(d1)#<br>
#month(d2)#<br>
</cfoutput>

You should use always use code such as

<cfset d1 = lsParseDateTime("2/3/2007")>
<cfset d2 = lsParseDateTime("13/3/2007")>
<cfoutput>
#month(d1)#<br>
#month(d2)#<br>
</cfoutput>

Which, here in Australia, correctly displays:

3

3

This entry was posted in ColdFusion and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.

7 Comments

  1. Brian Hendel
    Posted September 13, 2007 at 6:22 am | Permalink

    Very cool! I’ve noticed this issue a couple of times, and I will be parsing my dates manually whenever I can from now on.

    -Brian

  2. Posted October 30, 2007 at 6:15 am | Permalink

    So the code:

    <cfset myDate = "23/10/2006">
    <cfoutput>#myDate#</cfoutput>

    simply displays: 23/10/2007

    ********************************************

    Does it really display this? ;-)

  3. Posted October 31, 2007 at 1:41 am | Permalink

    Hi Sebastiaan, did you mean "23/10/2006" for the display? Yes, this is stored internally as a string so prints out exactly as originally set.

  4. Posted October 31, 2007 at 2:22 am | Permalink

    Hi Kevan, I was referring to the typo (in the example you refer to in your retort). You set the year to be 2006 and then say it will print 2007 ;-) Silly of me to point out such a small thingy in a further excellent article, but hey, that’s me!

  5. Posted October 31, 2007 at 2:33 am | Permalink

    Doh! Fixed!

  6. vijayvijay
    Posted February 24, 2011 at 4:04 pm | Permalink

    I am using CreateODBCDateTime() function to create Simple format as
    (mm/dd/yyyy- h:mm tt). But, when I am dumping the file, format is changing to
    {ts ’2011-08-06 00:00:00′} . I want it to appear as formated. I have used DateFormat() along with CreateODBCDateTime().
    Please help. Thanks in advance.

    Vijay.

  7. Posted March 6, 2011 at 8:36 pm | Permalink

    Hi Vijay, sorry, bit of a slow reply. Sounds like you need to use createDateTime() rather than createODBCDateTime().

    createDateTime() creates a real date object

    createODBCDateTime() converts a real date object into an ODBC date string

    In your case, perhaps you need something like this:

    <cfset date = createDateTime(2011,3,7,9,15,0)>
    <cfset formattedDate = dateFormat(date,"mm/dd/yyyy-") & " " & timeFormat(date,"h:mm tt")>
    <cfoutput>#formattedDate#</cfoutput>