Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Technical Questions
Message Icon Topic: DateDiff returning wrong value Post Reply Post New Topic
<< Prev Page  of 2
Author Message
huddy33
Groupie
Groupie


Joined: 23 Apr 2009
Location: United States
Online Status: Offline
Posts: 54
Quote huddy33 Replybullet Posted: 16 May 2012 at 3:44am
Because of the way this data is used, we have to count each day separately, not as a day+day=1 day.  Rkrowland, in your example of 4/10-4/17, it would be 10, 11, 12, 13, 14, 15, 16, 17, equaling 8 days that need to be charged.

I worked it out with a simple formula that is testing as accurate.  I still don't understand the root of the problem, but oh well.
IP IP Logged
rkrowland
Senior Member
Senior Member
Avatar

Joined: 20 Dec 2011
Location: England
Online Status: Offline
Posts: 259
Quote rkrowland Replybullet Posted: 16 May 2012 at 4:50am
Exactly 8 days would need to be charged... but if you did a datediff on that date range it would return 7. ie you need to put +1 on the end of your datediff formula - What Dblank said in his first post.

Edited by rkrowland - 16 May 2012 at 4:50am
IP IP Logged
huddy33
Groupie
Groupie


Joined: 23 Apr 2009
Location: United States
Online Status: Offline
Posts: 54
Quote huddy33 Replybullet Posted: 16 May 2012 at 5:16am
That is how I first approached this issue before coming here.  The problem was that it was adding an extra day to programs where it didn't need to.  I added in an if statement comparing the month(startdate) and all is well.  Thanks to both of you for trying.
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 16 May 2012 at 5:42am
you can add an if statement to use a datediff('m',start,end) to see if a month is straddled or even multiple months are straddled and use that result to alter the day diff result as desired.
however, what both Ryan and I were trying to get at is that your written description of the problem was not matching the math as we understand it, therefore we were suggesting a solution that seems to match the needed math. I am not saying your solution is wrong but am cautioning you that it does not seem to match your description of your desired result.
Datediff('d',start,end) will always calculate the same way regardless of straddling over months or years. By choosing to add an if statement you are not doing consistent comparisons of date ranges.
IP IP Logged
huddy33
Groupie
Groupie


Joined: 23 Apr 2009
Location: United States
Online Status: Offline
Posts: 54
Quote huddy33 Replybullet Posted: 16 May 2012 at 5:48am
I guess I don't really understand why the datediff is not calculating all of the days regardless. I know you say it is, but in the instances where it spans two months, it is missing a day. I realize my solution is not consistent, but the typical datediff is not yielding the "correct" result in this case.
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 16 May 2012 at 5:56am

my point is that datediff does calculate consistently but your description of how you want to count things is inconsistent.

use a one calendar day comparison
4-30-12 to 5-1-2012 is 1 day using datediff but you want 2 days
5-1-2012 to 5-2-2012 is also 1 day using datediff and you want 1 day
that is not consistent
IP IP Logged
<< Prev Page  of 2
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.031 seconds.