Announcement

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

  • Calculating difference between two times both in (hours: minutes: seconds)

    Hello everyone,

    Can someone please provide some guidance on how I can calculate the difference in minutes between these two times?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str8(startime_x endtime_x)
    "12:31:08" "12:41:27"
    "10:58:34" "11:02:15"
    "14:00:29" "14:03:57"
    "12:01:14" "12:05:48"
    "13:13:46" "13:16:38"
    "18:51:45" "18:53:44"
    "14:08:00" "14:12:19"
    "14:43:25" "14:45:40"
    "13:12:29" "13:15:21"
    "07:39:32" "07:44:04"
    "08:07:34" "08:10:54"
    "14:03:17" "14:07:10"
    "12:22:15" "12:26:42"
    "02:19:12" "02:22:57"
    "11:22:15" "11:27:02"
    "17:37:13" "17:42:16"
    "12:36:59" "12:39:15"
    "10:58:35" "11:00:25"
    "13:58:45" "14:01:30"
    "23:55:31" "23:57:47"
    "12:06:49" "12:09:35"
    "00:15:09" "00:19:11"
    "12:02:25" "12:11:48"
    "10:38:56" "10:41:43"
    "10:56:02" "11:00:57"
    end

  • #2
    Well, you can't calculate anything with strings. So the first step is to get real numeric time variables to work with. You don't say how you want the difference reported. The code below gives it to you in minutes (including fractions of minutes), but you can change that if you wish: see -help clockdiff_frac(), or clockdiff() if you want the answer as an integer.

    Code:
    gen double start = clock(startime_x, "hms")
    assert missing(start) == missing(startime_x)
    gen double end = clock(endtime_x, "hms")
    assert missing(end) == missing(endtime_x)
    format start end %tcHH:MM:SS
    
    gen diff = clockdiff_frac(start, end, "m")

    Comment


    • #3
      A direct attack is also possible:

      Code:
      . gen times = 3600 * real(substr(start, 1, 2)) + 60 * real(substr(start, 4, 2)) + real(substr(start, -2, 2))
      
      . gen timee = 3600 * real(substr(end, 1, 2)) + 60 * real(substr(end, 4, 2)) + real(substr(end, -2, 2))
      
      . gen diff = timee - times 
      
      . 
      . list, sep(0)
      
           +--------------------------------------------+
           | starti~x   endtim~x   times   timee   diff |
           |--------------------------------------------|
        1. | 12:31:08   12:41:27   45068   45687    619 |
        2. | 10:58:34   11:02:15   39514   39735    221 |
        3. | 14:00:29   14:03:57   50429   50637    208 |
        4. | 12:01:14   12:05:48   43274   43548    274 |
        5. | 13:13:46   13:16:38   47626   47798    172 |
        6. | 18:51:45   18:53:44   67905   68024    119 |
        7. | 14:08:00   14:12:19   50880   51139    259 |
        8. | 14:43:25   14:45:40   53005   53140    135 |
        9. | 13:12:29   13:15:21   47549   47721    172 |
       10. | 07:39:32   07:44:04   27572   27844    272 |
       11. | 08:07:34   08:10:54   29254   29454    200 |
       12. | 14:03:17   14:07:10   50597   50830    233 |
       13. | 12:22:15   12:26:42   44535   44802    267 |
       14. | 02:19:12   02:22:57    8352    8577    225 |
       15. | 11:22:15   11:27:02   40935   41222    287 |
       16. | 17:37:13   17:42:16   63433   63736    303 |
       17. | 12:36:59   12:39:15   45419   45555    136 |
       18. | 10:58:35   11:00:25   39515   39625    110 |
       19. | 13:58:45   14:01:30   50325   50490    165 |
       20. | 23:55:31   23:57:47   86131   86267    136 |
       21. | 12:06:49   12:09:35   43609   43775    166 |
       22. | 00:15:09   00:19:11     909    1151    242 |
       23. | 12:02:25   12:11:48   43345   43908    563 |
       24. | 10:38:56   10:41:43   38336   38503    167 |
       25. | 10:56:02   11:00:57   39362   39657    295 |
           +--------------------------------------------+

      Comment


      • #4
        For the benefit of others who may see this topic at a later date, let me add to the advice given above.

        The direct attack shown in post #3 can only take you so far. If you will be working with date or time data, you are well advised to read the detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF. It's a long read and there is a lot there. But it will acquaint you with the philosophy of Stata's datetime variables, the functions available to create and transform them, and how to calculate with them. You won't remember everything, but at the end you will probably be able to remember what functions are called for when the need for one arises, and then you can rely on the help datetime documentation to refresh your memory about the details. The time you spend doing this will be amply and soon repaid.

        All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        Comment


        • #5
          I don't disagree with any of the excellent advice in #2 or #4. #3 was really for anyone who thinks that the information you need is all there in the time strings -- and wonders whether taking the calculation in millisecond form is needed when the resolution is at most seconds.

          Here is another approach in the same spirit.

          Code:
          split start, p(:) destring gen(start)
          split end, p(:) destring gen(end)
          gen wanted = 3600 * (end1 - start1) + 60 * (end2 - start2) + end3 - start3

          Comment

          Working...
          X