Determine the difference between two DateTimes, only counting opening hours

前端 未结 6 2136
梦毁少年i
梦毁少年i 2020-12-03 07:36

For our support software in C#, I need to determine the time span between two DateTimes, but I only want opening hours counted (i.e. weekdays from 09:00 to 17:00).

S

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 08:29

    You can try this Algorithm:

    1. Calculate total No Of Hours (lets say TotalHours = (Date2 - Date1).TotalHours)
    2. Calculate No of Holidays (holidays, includes weekends & other holidays)
    3. Calcualte No of Working days ( (Date2-Date1).TotalDays - Holidays) as int

    Working Hours = TotalHours - 24*holidays - 16*WorkingDays

    (16 = Time difference between 5pm and 9am the next day)

提交回复
热议问题