• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1657
  • Last Modified:

DateDIff / DateCompare converting and handling coldfusion time formats


Hi guys, can anyone tell me whats wrong with this following code?

Im grabbing a date from a database and setting it as a variable called "StartDate" with the date mask of dd/mm/yyyy.

Im then grabbing today's date from the server and setting that as "theDate". Now this seems to work..... the two dates output as

StarDate = 01/07/2010
TheDate = 05/01/2010

Then Im looking at the dateDiff between the two (see first code example - which seems to work ok.)

The two dates above output a datDiff of 114 days which is great.

Then I want to test the date with other made up dates so I set "theDate" variable to something made up (see example 2).... and thats where I encounter problems, coldfusion is treating the dd as months so i think im converting the date wrong or something. Please take a look and let me know your thoughts.


thanks
<!--- Example one --->

<!--- set variables StartDate and theDate --->

<cfset StartDate = dateformat("#getStartDateSem2.StartDate#","dd/mm/yyyy")>

<cfset theDate = dateformat(now(),"dd/mm/yyyy")>

Date Difference: #dateDiff("d","#StartDate#","#theDate#")#




<!--- Example 2 for testing different dates manually --->

<!--- set variables StartDate and theDate --->

<cfset StartDate = dateformat("#getStartDateSem2.StartDate#","dd/mm/yyyy")>

<cfset theDate = dateformat(createDate("2010","07","02"),"dd/mm/yyyy")>

Date Difference: #dateDiff("d","#StartDate#","#theDate#")#

Open in new window

0
CurtinProp
Asked:
CurtinProp
2 Solutions
 
Andrew MaurerCommented:
I would say, only use dateformat for the displayed date, let coldfusion compare the dates without you trying to format them.

0
 
_agx_Commented:
I agree with @zadoc.  

> Im grabbing a date from a database and setting it as a variable called "StartDate" with
> the date mask of dd/mm/yyyy.

That is your first problem.  When you pull the date from your date/time column, it is a date object at that point. As soon as you use DateFormat() you are actually converting that date object into a _string_.   That is where the problems start.

> <cfset StartDate = dateformat("#getStartDateSem2.StartDate#","dd/mm/yyyy")>
> Date Difference: #dateDiff("d","#StartDate#","#theDate#")#

The DateDiff() function actually expects a _date_ (not a string).  So when you use it, ColdFusion must first convert the string #StartDate# back into a _date_ object.  This is problematic because most CF date functions assume U.S. dates.  So even though you're seeing the string "01/07/2010", CF actually interprets this as January 7th (not July 1st).  So that is why you get the wrong results.


1) Always use date/time objects for comparisons, not strings.  
2) If you need locale specific behavior for dates, use the LS date functions

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6817.html
0
 
CurtinPropAuthor Commented:
Thanks guys, I'll compare the dates and then format the output.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now