Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

DateDiff problem

Guest
Apr 22, 2006 Apr 22, 2006
This message is either a "FYI" or a way to embarass myself. I'm probably missing something very simple, or missing the point of the DateDiff function, so my apologies if this turns out to be a dumb posting.

I created a series of cfif/else statements to display a different text message depending on how many days remain in a survey. I used the DateDiff function. It works fine, counting down from 10 to zero days, but then it calculates the next instance as zero days again.

I solved my problem by switching to the DayOfYear function and that produces the correct result, but I'm perplexed why the DateDiff approach didn't work. Here's my DateDiff code:

<cfset CurrentYear = Year(Now())>
<cfset SVRSurveyEndDate = '4-20-#CurrentYear#'>
<cfset SVRSurveyDays = DATEDIFF('d',Now(),SVRSurveyEndDate + 1)>

If you change SVRSurveyEndDate one day at a time and output the result, you will see the results as +2, +1, 0, 0, -1 etc.

I solved my problem using this next line instead of the third line above:
<cfset SVRSurveyDays = (DayOfYear(SVRSurveyEndDate)) - (DayOfYear(Now())) + 1>

But I don't know why the DateDiff function produces the two zero results. I tried including hrs/min/sec in line 2 and varying the time, but that still produced two zero results.
298
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 22, 2006 Apr 22, 2006
Are you sure the final right bracket is in the right spot on this line?
<cfset SVRSurveyDays = DATEDIFF('d',Now(),SVRSurveyEndDate + 1)>

The reason you are getting zero twice is that you are comparing now(), which has a time component to other dates that don't.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 22, 2006 Apr 22, 2006
mikescott wrote:
> But I don't know why the DateDiff function produces the two zero results. I
> tried including hrs/min/sec in line 2 and varying the time, but that still
> produced two zero results.

w/out spending any time w/your logic, using day as the dateDiff datepart, you'll
always get an integer day rounded *down* ie 1.9999 days is 1 day as far as cf is
concerned:

<cfoutput>
#dateAdd("d",1.99999,now())#
</cfoutput>

produces a date only 1 day in the future.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2006 Apr 23, 2006
LATEST
> But I don't know why the DateDiff function produces the two zero results.
A reason why

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources