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

DateDiff confusion

LEGEND ,
Nov 07, 2014 Nov 07, 2014

Copy link to clipboard

Copied

Hello, everyone,

I'm looping through all the days of the current month, using DateDiff to display the difference in days between the current date and the date of the loop, and I'm getting TWO days showing a difference of 0.

<cfloop from="1" to="#DaysInMonth('01-Nov-14')#" index="ndx">

#DateDiff('d',DateFormat(ndx & '-Nov-14'),now())#<br />

</cfloop>

The output will be

6

5

4

3

2

1

0 <!--- (today, Nov 7) --->

0 <!--- (tomorrow, Nov 8) --->

-1

-2

-3

-4

How can there be two days with a difference of 0?

V/r,

^_^

Views

1.1K

Translate

Translate

Report

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

correct answers 1 Correct answer

Advocate , Nov 07, 2014 Nov 07, 2014

DateDiff determines the number of complete "datepart" units between the two dates. In your code you are comparing two dates formatted a different way. now() will include the time as well, which when compare to the the other date you formatted (which would have a time of midnight) has a difference between the dates of 7.5 hours (by my time). As you specified 'd' and this difference is not a full 24 hours the difference is 0.

If you do this:

#DateDiff('d',DateFormat(ndx & '-Nov-14'), DateFormat(now

...

Votes

Translate

Translate
Advocate ,
Nov 07, 2014 Nov 07, 2014

Copy link to clipboard

Copied

DateDiff determines the number of complete "datepart" units between the two dates. In your code you are comparing two dates formatted a different way. now() will include the time as well, which when compare to the the other date you formatted (which would have a time of midnight) has a difference between the dates of 7.5 hours (by my time). As you specified 'd' and this difference is not a full 24 hours the difference is 0.

If you do this:

#DateDiff('d',DateFormat(ndx & '-Nov-14'), DateFormat(now()) )#

You would get the result you are expecting. You will be comparing like for like date formats.

Votes

Translate

Translate

Report

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 ,
Nov 07, 2014 Nov 07, 2014

Copy link to clipboard

Copied

LATEST

You can't see it, right now, but I'm facepalming myself.

I should have taken that into consideration.  I'm one of the few in this dept that will trim(UPPER(query.column)) and trim(ucase(cfValue)) to make absolutely sure a match is correct, and I completely flaked on the time being appended to the date.

Thanks, haxtbh!

^_^

Votes

Translate

Translate

Report

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
Documentation