Well, while you'd think it should be 10, I'm more curious why it's showing
8, when according to the docs (which are pretty explicit about DateDiff and
use of the "ww" datepart for weeks) it should be 9:
If interval is Week ("ww"), however, the DateDiff function returns the
number of calendar weeks between the two dates. It counts the number of
Sundays between date1 and date2. DateDiff counts date2 if it falls on a
Sunday; but it doesn't count date1, even if it does fall on a Sunday.
Some code demonstrates things
<cfset date1="jan 14 2002">
<cfset date2="Mar 18 2002">
<cfoutput>
#date1# falls on a #dayofweekasstring(dayofweek(date1))#<br>
#date2# falls on a #dayofweekasstring(dayofweek(date2))#<br>
Weeks between them: #datediff("ww", date1, date2)#
</cfoutput>
The result is:
jan 14 2002 falls on a Monday
Mar 18 2002 falls on a Monday
Weeks between them: 8
It reports 8, as you say, but I find that counting the number of Sundays
between those dates, I get 9. And you want 10. :-)
The mistake between 8 and 9 seems to be a bug, or a mistake in the docs (I
think the latter, because if you simply count Mondays from Monday Jan 14,
when you get to Mon Mar 18 you count 9. Now, you say you want 10. Here's one
way to get 10:
Weeks between them: #ceiling(evaluate(datediff("d", date1, date2)/7))#
This gets 9 with your dates, by dividing the number of days between the
dates by 7, then the "ceiling" takes the integer part of the result (no
fraction/remainder wanted) and raises it to the next whole number if there
was a remainder. We don't want to round.
Now, you wanted 10. I'd argue that this a semantic debate about what
"between" means. But, from the algorithm in CF, it won't matter if you up
the end date by a day. It still counts Sundays between the dates. In my
code, since your second date always means "including this date", you can
simply change the algorithm to add a day, as in:
Weeks between them: #ceiling(evaluate(datediff("d", date1,
dateadd("d",1,date2))/7))#
Note that I've just slipped in a dateadd to add a day to the second date.
This also demonstrates the reason for the ceiling. Without it, the answer
would be 9.14285714286. And again you don't want to round down. The ceiling
makes it 10.
Hope that helps. CF functions are really quite powerful!
/charlie
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
Behalf Of Graham Faulkner
Sent: Tuesday, January 15, 2002 1:20 PM
To: [EMAIL PROTECTED]
Subject: [CFTALKTor] DateDiff question
Hi there,
I have a site where I need to specify the amount of weeks a course is
scheduled. For example, there is a course that meets every Monday night
from January 14 until March 18. However, when I use #DateDiff("WW",
StartDate, EndDate)# it calculates it as 8 weeks, instead of the 10 it needs
to be because there are classes on the start and end dates. Am I using the
function incorrectly? Should I be using something else to calculate this?
Thanks in advance for your help.
Blessings,
Graham
------------------------------------------------------------
Graham Faulkner, BBA
President
Global Audience Communications
Web design with YOUR world in mind!
Web: http://www.GlobalAudienceCommunications.com
Email: [EMAIL PROTECTED]
Tel./Fax: 519-880-9184
Cell: 519-496-0887
-
You are subscribed to the CFUGToronto CFTALK ListSRV.
This message has been posted by: "Graham Faulkner"
<[EMAIL PROTECTED]>
To Unsubscribe, Please Visit and Login to http://www.CFUGToronto.org/
Manager: Kevin Towes ([EMAIL PROTECTED]) http://www.CFUGToronto.org/
This System has been donated by Infopreneur, Inc.
(http://www.infopreneur.net)
-
You are subscribed to the CFUGToronto CFTALK ListSRV.
This message has been posted by: "charlie arehart" <[EMAIL PROTECTED]>
To Unsubscribe, Please Visit and Login to http://www.CFUGToronto.org/
Manager: Kevin Towes ([EMAIL PROTECTED]) http://www.CFUGToronto.org/
This System has been donated by Infopreneur, Inc.
(http://www.infopreneur.net)