简体   繁体   中英

How to calculate handling time or response time between two dates in Excel or R?

I work at a company which serves as a doctor booking platform. We usually deal with bookings. And the bookings are for multiple clinics. For every bookings we have a time when it was created and a time when it was handled. I want to calculate the response time. Which should be the difference between handled and created but within the clinic hours.

For example, a booking is made on 7th Aug 2020(Friday) at 11 am and is handled on 10th Aug 2020(Monday) at 5 PM, the response time within clinic hours should be 88,200 seconds or 24.5 hours. I have attached clinic's working hours. I have to do this for hundreds of clinics with their own schedules. I would highly appreciate your help here.

Clinic Schedule

Clinic_name <- c("ABC","ABC","ABC","ABC","ABC","ABC","ABC")
DOW <- c(1:7)
day <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
Starting_Time_1 <- c("8:30:00","8:30:00","8:30:00","8:30:00",NA,"8:30:00","8:30:00")
Ending_Time_1 <- c("12:00:00","12:00:00","12:00:00","12:00:00",NA,"12:00:00","12:00:00")
Starting_Time_2 <- c("16:30:00","16:30:00","16:30:00","16:30:00","16:30:00","16:30:00","16:30:00")
Ending_Time_2 <- c("21:00:00","21:00:00","21:00:00","21:00:00","21:00:00","21:00:00","21:00:00")

abc_timing <- cbind(Clinic_name,DOW,day,Starting_Time_1,Ending_Time_1,Starting_Time_2,Ending_Time_2)

abc_timing <- data.frame(abc_timing)
library(tidyverse)
library(lubridate)

df = tibble(
  booking = c("7th Aug 2020(Friday) at 11 am", "8th Aug 2020(Friday) at 13 am"),
  handled = c("10th Aug 2020(Monday) at 5 PM", "9th Aug 2020(Friday) at 06 am")
) %>% 
  mutate(booking = dmy_h(booking),
         handled = dmy_h(handled))


df %>% 
  mutate(gap = (handled - booking) / dhours(1))
# # A tibble: 2 x 3
#   booking             handled               gap
#   <dttm>              <dttm>              <dbl>
# 1 2020-08-07 11:00:00 2020-08-10 05:00:00    66
# 2 2020-08-08 13:00:00 2020-08-09 06:00:00    17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM