It is known that the start time and work date are used to calculate the end time of a business day:
C #:
Public datetime getdate (datetime dtbegin, int workdays)
{
Datetime endtime;
Int J = 0;
Int I = 0;
Do
{
Endtime = dtbegin. Date. adddays (I );
I = I + 1;
If (endtime. dayofweek! = System. dayofweek. Saturday) & (endtime. dayofweek! = System. dayofweek. Sunday ))
{
J = J + 1;
}
}
While (J! = Workdays );
Return endtime;
}
SQL:
Create Function f_workdayadd (
@ Date datetime, -- base date
@ Workday int -- the number of working days to be added
) Returns datetime
As
Begin
Declare @ BZ int
-- Increase the number of days in a week
Select @ Bz = case when @ workday <0 then-1 else 1 end
, @ Date = dateadd (Week, @ workday/5, @ date)
, @ Workday = @ workday % 5
-- Increase the number of working days not for the whole week
While @ workday <> 0
Select @ date = dateadd (day, @ BZ, @ date ),
@ Workday = case when (@ datefirst + datepart (weekday, @ date)-1) % 7 between 1 and 5
Then @ workday-@ BZ else @ workday end
-- Avoid the processing date staying in the non-work period
While (@ datefirst + datepart (weekday, @ date)-1) % 7 in (0, 6)
Set @ date = dateadd (day, @ BZ, @ date)
Return (@ date)
End