Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: DateTime Calculation Post Reply Post New Topic
Author Message
korey_premier
Newbie
Newbie


Joined: 23 Jun 2014
Location: United States
Online Status: Offline
Posts: 8
Quote korey_premier Replybullet Topic: DateTime Calculation
     Posted: 07 Jul 2014 at 10:30am
I wanted to check one more time to see if this group has another other suggestions. It seems like I'm so close!

See below (I've corrected my formula per the suggestions in previous posts but still not quite there.)

Hello!
I have discovered several great posts in this forum that help with DateTime calculations but I'm having trouble using one of these formulas in a report.
I need to calculate a number of hours between a task creation date and a task completion date. The calculation should exclude non-business hours and weekends. The formula seen below works well until I get to a weekend. Each workday (07:00-17:00) amounts to 10 hours added to the total. Both days of each weekend should be ignored but instead, the formula only ignores one of the days. For entries that cross weekends, my total is exactly 10 hours too high.

I'm sure someone with more experience would be able to spot my error quickly.

Thanks in advance for any help!
Korey

DATETIMEVAR StDate:= {TASK.CREATE_DATE};
DATETIMEVAR EndDate:= {TASK.COMPLETED_DATE};
NUMBERVAR Weeks;
NUMBERVAR Days;

TIMEVAR SLA_Open := TIME(7,0,0);
TIMEVAR SLA_Close := TIME(17,00,0);
NumberVar WeekendTime ;
NUMBERVAR NonWorkTime ;
NUMBERVAR Weeks;
NUMBERVAR Days;


IF WeekDayName(DAYOFWEEK(StDate)) = "Saturday" THEN
   StDate:= DATETIMEVALUE(DATE(DATEADD('D',2,StDate)) , SLA_Open);

IF WeekDayName(DAYOFWEEK(StDate)) = "Sunday" THEN
   StDate:= DATETIMEVALUE(DATE(DATEADD('D',1,StDate)) , SLA_Open);

IF TIME(StDate) > SLA_Close   THEN
    StDate := DATETIMEVALUE(DATE(StDate) , SLA_Close);
      
IF TIME(StDate) < SLA_Open THEN
    StDate := DATETIMEVALUE(DATE(StDate) , SLA_Open);


IF WeekDayName(DAYOFWEEK(endDate)) = "Saturday" THEN
endDate = DATETIMEVALUE(DATE(DATEADD('D',2,endDate)) , SLA_Open);

IF WeekDayName(DAYOFWEEK(endDate)) = "Sunday" THEN
endDate = DATETIMEVALUE(DATE(DATEADD('D',1,endDate)) , SLA_Open);

IF TIME(endDate) > SLA_Close   THEN
    endDate := DATETIMEVALUE(DATE(endDate) , SLA_Close);
      
IF TIME(endDate) < SLA_Open THEN
    endDate := DATETIMEVALUE(DATE(endDate) , SLA_Open);

Weeks:= (Truncate (EndDate - dayofWeek(EndDate) + 1 - (StDate - dayofWeek(StDate) + 1)) /7 ) * 5;

Days := DayOfWeek(EndDate) - DayOfWeek(StDate) + (if DayOfWeek(StDate) = 1 then -1 else 0) +
                                                  (if DayOfWeek(EndDate) = 7 then -1 else 0);     

// Non Worktime on Business days
NonWorkTime := DATEDIFF("N",DATETIMEVALUE(CurrentDate, SLA_Close),DATETIMEVALUE(CurrentDate+1,SLA_Open)) * (Weeks + Days);


//a weekend in minutes is Count of saturdays and sundays   * 24 hours * 60 minutes
WeekendTime := (DateDiff("Ww", stDate ,Enddate , crSaturday) + DateDiff("Ww", stDate, Enddate, crSunday)) * 24 * 60;

(DATEDIFF("N", stDATE, endDate)- NonWorkTime - WeekendTime)/60
IP IP Logged
hello
Groupie
Groupie
Avatar

Joined: 05 Feb 2014
Online Status: Offline
Posts: 85
Quote hello Replybullet Posted: 10 Jul 2014 at 10:46am
IF WeekDayName(DAYOFWEEK(endDate)) = "Saturday" THEN
endDate = DATETIMEVALUE(DATE(DATEADD('D',2,endDate)) , SLA_Open);

IF WeekDayName(DAYOFWEEK(endDate)) = "Sunday" THEN
endDate = DATETIMEVALUE(DATE(DATEADD('D',1,endDate)) , SLA_Open);

Don't you need to use := instead of = in two of those places?
IP IP Logged
korey_premier
Newbie
Newbie


Joined: 23 Jun 2014
Location: United States
Online Status: Offline
Posts: 8
Quote korey_premier Replybullet Posted: 10 Jul 2014 at 11:35am
I appreciate the suggestion but the script editor did not like the added colons.

I have found a work around that completes the report and returns accurate information but still can't imagine why the original formula will not work. See below.

(DATEDIFF("N", stDATE, endDate)- NonWorkTime - (if WeekendTime > 0 then Weekendtime +600 else Weekendtime))/60
IP IP Logged
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