Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : 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: 24 Jun 2014 at 3:16am
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',1,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',1,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
Gurbs
Senior Member
Senior Member
Avatar

Joined: 16 Feb 2012
Location: Ireland
Online Status: Offline
Posts: 216
Quote Gurbs Replybullet Posted: 24 Jun 2014 at 3:37am
IF WeekDayName(DAYOFWEEK(StDate)) = "Saturday" THEN
   StDate:= DATETIMEVALUE(DATE(DATEADD('D',1,StDate)) , SLA_Open);

You are saying that you want to add 1 day if the start day is Saturday. This makes the start day on Sunday instead of Monday. Change this to
IF WeekDayName(DAYOFWEEK(StDate)) = "Saturday" THEN
   StDate:= DATETIMEVALUE(DATE(DATEADD('D',2,StDate)) , SLA_Open);
and you should be good

Edit: Same counts for this part:
IF WeekDayName(DAYOFWEEK(endDate)) = "Saturday" THEN
endDate = DATETIMEVALUE(DATE(DATEADD('D',1,endDate)) , SLA_Open);

Edited by Gurbs - 24 Jun 2014 at 3:39am
IP IP Logged
korey_premier
Newbie
Newbie


Joined: 23 Jun 2014
Location: United States
Online Status: Offline
Posts: 8
Quote korey_premier Replybullet Posted: 24 Jun 2014 at 4:22am
Thanks for the suggestion Gurbs.
Were you able to get this to work?

The 'DATEADD' amount was one of the things I noticed that didn't seem to fit but adjusting the amount to 2 doesn't seem to change the result.
Any other thoughts?
IP IP Logged
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.