Skip to main content
Inspiring
June 20, 2018
Answered

DateCompare returning incorrect result after moving to CF11 from CF9

  • June 20, 2018
  • 2 replies
  • 1932 views

I have a page which has been the same since CF7. Recently we moved our application to CF11 to support TLS1.2 and I've since noticed that this one particular bit of code has stopped functioning correctly and it's lead me down a weird road.

This is the following code block. It's meant to create date/time ranges for conditional statements and compare the current time against those ranges, performing a certain task.

<!--- Set date --->

<cfset thisDate = getDate()>

<!--- Determine what flight they should see. --->

<!--- If it's after 9am and before 10pm --->

<cfif thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9,0,0)

AND

thisDate LT CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21,0,0)>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<!--- If it's after 10pm --->

<cfelseif thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21,0,0)>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

<cfset d2 = DateAdd("d",1,d2)>

<!--- If it's before 9am --->

<cfelse>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<cfset d1 = DateAdd("d",-1,d1)>

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

</cfif>

The above code has always functioned correct, however since the move I've noticed it's no longer evaluating the current date/time as falling within the correct ranges. This morning at 11:15am I checked and it was not firing the first if statement, but instead firing the else statement. Meaning it thought it was before 9:00am.

I assumed this meant that perhaps in CF11 I'm not able to just evaluate the times using LTE and GTE. So I thought instead that I would test using DateCompare. So I wrote the following:

<cfset thisDate = getDate()>

<cfset date1 = CreateODBCDateTime(thisDate)>

<cfset date2 = CreateODBCDateTime(CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9,0,0))>

<cfoutput>

date1: #date1#<br />

date2: #date2#<br />

<br />

DateCompare: #DateCompare(date1,date2)#<br />

GTE Evaluation: #date1 GTE date2#<br />

</cfoutput>

Now this is where it gets weird. The output from the above is:

date1: {ts '2018-06-20 15:12:00'}

date2: {ts '2018-06-20 09:00:00'}

DateCompare: -1

GTE Evaluation: NO

The date compare should output "1" and the GTE evaluation should output "YES" since they are the same day and date1 is 3pm and date2 is 9am.

Can anyone help me out here? This is mind boggling for me.

Thanks so much,

Mike

This topic has been closed for replies.
Correct answer EddieLotter

It's probably a transcription error, but there is no GetDate() function in CF, you probably mean Now() right?

Also, your comments talk about 10PM, but 21:00 is 9PM.

It's also worth pointing out that the values you assign to d1 and d2 are strings until you do some date arithmetic on one or the other. This might be causing you problems later. Try to stick with date types throughout.

Other than that, the code works as expected for me on CF 11.

Add the following line at the end of your example code to see all your variables:

<cfdump var="#variables#" showudfs="false" >

Cheers

Eddie

2 replies

itisdesign
Inspiring
June 26, 2018

Hi Mike,

Could you please run the following in CF9 and report the output?

<cfscript>

  time1 = time2 = createDateTime(2018,1,2,3,4,5);

  time2 = dateConvert("local2utc", time2);

  writeOutput(SERVER.ColdFusion.productVersion & '<br>');

  writeOutput(time1+0 & '<br>' & time2+0 & '<br>');

  writeOutput(dateTimeFormat(time1, "yyyy-mm-dd HH:nn:ss z") & '<br>');

  writeOutput(dateTimeFormat(time2, "yyyy-mm-dd HH:nn:ss z") & '<br>');

  writeOutput(time1 is time2);

</cfscript>

Thanks!,

-Aaron

mr. modusAuthor
Inspiring
June 26, 2018

Unfortunately the codebase is so longer active on CF9. Only CF11.

Mike

On Mon, Jun 25, 2018 at 11:59 PM itisdesign <forums_noreply@adobe.com>

EddieLotter
EddieLotterCorrect answer
Inspiring
June 20, 2018

It's probably a transcription error, but there is no GetDate() function in CF, you probably mean Now() right?

Also, your comments talk about 10PM, but 21:00 is 9PM.

It's also worth pointing out that the values you assign to d1 and d2 are strings until you do some date arithmetic on one or the other. This might be causing you problems later. Try to stick with date types throughout.

Other than that, the code works as expected for me on CF 11.

Add the following line at the end of your example code to see all your variables:

<cfdump var="#variables#" showudfs="false" >

Cheers

Eddie

mr. modusAuthor
Inspiring
June 21, 2018

Hi Eddie,

Thanks for the reply. The getDate() function is a function I created to adjust for the timezone difference between where the server is located and what the local time is, within the organization.

<cfscript>

function getDate() {

      return DateAdd('h',(application.GMTOffset*-1),DateConvert("local2utc", Now()));

}

</cfscript>

Yes, sorry, I do actually mean 9am and 9pm, not 10pm.

I wrapped the times ParseDateTime() and now it's working as expected. That's a bit of a bummer that it functions differently.

Thanks for much for the help.

EddieLotter
Inspiring
June 21, 2018

I'm glad you got it sorted out, but I'm not clear which function you say is working differently. If you have the time, would you mind elaborating?

Cheers

Eddie