Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculate difference between two clock times

    Hi,

    I have string data on start and end times of an activity in "hhmm" format. I want to find the total time spent on the task. However, I am facing a problem.


    Code:
    gen seconds= 0
    split time_from, p(:)
    destring time_from1 time_from2, replace
    rename (time_from1 time_from2)(hour minute)
    gen from_time = hms(hour,minute,second)
    format %tcCCYY-NN-DD_hh:MM_AM from_time
    drop hour minute
    
    split time_to, p(:)
    destring time_to1 time_to2, replace
    rename (time_to1 time_to2)(hour minute)
    gen to_time = hms(hour,minute,second)
    //format %tcCCYY-NN-DD_hh:MM_AM to_time
    drop hour minute second
    Using this, I created the time spent.
    Code:
    gen time_spent = to_time - from_time
    however, the values are not in minutes. Please find data example here.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5(time_from time_to)
    "04:00" "08:00"
    "08:00" "08:30"
    "08:30" "09:00"
    "09:00" "10:00"
    "10:00" "10:30"
    "10:30" "11:00"
    "11:00" "11:30"
    "11:30" "15:00"
    "15:00" "15:30"
    "15:30" "18:00"
    "18:00" "19:00"
    "19:00" "19:30"
    "19:30" "20:30"
    "20:30" "21:00"
    "21:00" "04:00"
    "04:00" "07:30"
    "07:30" "08:00"
    "08:00" "08:30"
    "08:30" "10:00"
    "10:00" "10:30"
    "10:30" "11:00"
    "11:00" "15:00"
    "15:00" "15:30"
    "15:30" "16:00"
    "16:00" "18:30"
    "18:30" "19:00"
    "19:00" "19:30"
    "19:30" "21:00"
    "21:00" "04:00"
    "04:00" "08:00"
    "08:00" "08:30"
    "08:30" "09:00"
    "09:00" "10:00"
    "10:00" "10:30"
    "10:30" "11:00"
    "11:00" "11:30"
    "11:30" "16:00"
    "16:00" "17:00"
    "17:00" "19:00"
    "19:00" "19:30"
    "19:30" "20:00"
    "20:00" "22:00"
    "22:00" "04:00"
    "04:00" "07:00"
    "07:00" "07:30"
    "07:30" "08:00"
    "08:00" "10:00"
    "10:00" "10:30"
    "10:30" "11:00"
    "11:00" "11:30"
    "11:30" "16:00"
    "16:00" "16:30"
    "16:30" "18:30"
    "18:30" "19:30"
    "19:30" "20:00"
    "20:00" "21:30"
    "21:30" "04:00"
    "04:00" "07:00"
    "07:00" "07:30"
    "07:30" "08:00"
    "08:00" "09:00"
    "09:00" "09:30"
    "09:30" "10:00"
    "10:00" "12:00"
    "12:00" "15:00"
    "15:00" "18:00"
    "18:00" "20:00"
    "20:00" "20:30"
    "20:30" "21:00"
    "21:00" "04:00"
    "04:00" "08:00"
    "08:00" "08:30"
    "08:30" "09:00"
    "09:00" "10:00"
    "10:00" "10:30"
    "10:30" "11:00"
    "11:00" "13:00"
    "13:00" "16:00"
    "16:00" "18:00"
    "18:00" "19:30"
    "19:30" "20:00"
    "20:00" "21:00"
    "21:00" "04:00"
    "04:00" "06:00"
    "06:00" "06:30"
    "06:30" "07:00"
    "07:00" "08:00"
    "08:00" "09:00"
    "09:00" "09:30"
    "09:30" "10:00"
    "10:00" "12:00"
    "12:00" "13:00"
    "13:00" "16:00"
    "16:00" "18:00"
    "18:00" "18:30"
    "18:30" "19:30"
    "19:30" "20:00"
    "20:00" "21:00"
    "21:00" "21:30"
    "21:30" "04:00"
    end
    Thanks!

  • #2
    I didn't try to find your error(s). This seems more direct. Note that your code seems to do nothing to cope with times that straddle midnight. if some of your times extend over 3 or more days, the calculation needs more information.

    Code:
    gen from = 60 * real(substr(time_from, 1, 2)) + real(substr(time_from, 4, 2))
    
    gen to =  60 * real(substr(time_to, 1, 2)) + real(substr(time_to, 4, 2)) 
    
    gen difference = cond(to < from, to + 24 * 60 - from, to - from)
    
    sort difference 
    
    list

    Comment


    • #3
      Thanks Nick, that was much more straightforward than I thought.

      Comment

      Working...
      X