Announcement

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

  • how to convert to HH:MM:SS to numberic ( minutes ) ?

    Hi All ,
    I have the following data about time : differ_time with %tcHH:MM:SS

    differ_time
    00:59:36
    00:31:15
    00:08:58
    00:24:45
    00:54:11

    I want to calculate in number of minutes . please show me the ways !

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double differ_time
    3576000
    1875000
     538000
    1485000
    3251000
    end
    format %tcHH:MM:SS differ_time
    
    gen minutes = differ_time/msofminutes(1)
    In the future, when showing data examples, please use the -dataex- command to do so, as I have done here. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.


    Comment


    • #3
      Clyde gives excellent advice as always. The factor to divide by can also just be given directly as 1000 * 60 or 60000 or 6e4.

      Code:
      . di msofminutes(1)
      60000
      
      . di 6e4
      60000

      Comment


      • #4
        Hi Clyde Schechter !
        Thanks for your help !

        Comment


        • #5
          Hi ,
          I have another case about time : please help me

          In real data sex
          I'm now calculating time used between cases of actiontime , I used the code like

          gen double difference = actiontime-actiontime[_n+1] , the result are below

          But I think the result from 'difference' column are not correct format , How can I do correct format like 'correct_answer' with HH:MM:SS format
          actiontime difference correct_answer
          04-01-18 4:00 23:00:24 1:00:00
          04-01-18 5:00 23:28:45 0:31:00
          04-01-18 5:31 23:51:02 0:09:00
          04-01-18 5:40 23:35:14
          Here I put data sample using dataex

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double(actiontime difference)
               1838217647000            -3576000
               1838221223000            -1875000
               1838223098000             -538000
               1838223636000 -1485000.0002441406
          1838225121000.0002            -3251000
          1838228372000.0002            -1807000
          1838230179000.0002  -337999.9997558594
               1838230517000            -5675000
               1838236192000           -48202000
               1838284394000            -2097000
          end
          format %tcnn/dd/ccYY_hh:MM actiontime
          format %tcHH:MM:SS difference

          Comment


          • #6
            Here is correct table format :
            I would like to get 'correct_answer' right format , but when run the code

            gen double difference = actiontime-actiontime[_n+1]
            format %tcHH:MM:SS difference

            sr_n actiontime difference correct_answer
            1 04-01-18 4:00 23:00:24 1:00:00
            2 04-01-18 5:00 23:28:45 0:31:00
            3 04-01-18 5:31 23:51:02 0:09:00
            4 04-01-18 5:40 23:35:14 0:25:00
            5 04-01-18 6:05 23:05:49 0:54:00
            6 04-01-18 6:59 23:29:53 0:30:00
            7 04-01-18 7:29 23:54:22 0:06:00
            8 04-01-18 7:35 22:25:25 1:34:00
            9 04-01-18 9:09 10:36:38 1:24:00
            10 04-02-18 10:33 23:25:03

            In here is data example using dataex


            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float sr_n double(actiontime difference)
             1      1838217647000            -3576000
             2      1838221223000            -1875000
             3      1838223098000             -538000
             4      1838223636000 -1485000.0002441406
             5 1838225121000.0002            -3251000
             6 1838228372000.0002            -1807000
             7 1838230179000.0002  -337999.9997558594
             8      1838230517000            -5675000
             9      1838236192000           -48202000
            10      1838284394000            -2097000
            end
            format %tcnn/dd/ccYY_hh:MM actiontime
            format %tcHH:MM:SS difference

            Comment


            • #7
              For your data, negative differences between clock times show up, with %tc format, as times before midnight and, implicitly in your case, on 31 December 1959.

              Remember: Stata really does not ever know that any time you give is a duration or elapsed time, not a clock time.

              The display format %tc is for clock times and only by accident correct for (positive) elapsed times up to 24 hours.


              I don't understand why some of your original times are not integers.

              Negating the variable seems to get you some way towards where you want But (for example) your first clock time is really

              Code:
              . di %tc 1838217647000
              01apr2018 16:00:47
              Ignoring the 47 seconds with a display format showing hours and minutes won't round the values Stata holds as data. It only affects display results.

              Your "correct answers" seem to be calculated on the assumption that Stata ignores trailing seconds, but it won't.

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input double(actiontime difference)
                   1838217647000            -3576000
                   1838221223000            -1875000
                   1838223098000             -538000
                   1838223636000 -1485000.0002441406
              1838225121000.0002            -3251000
              1838228372000.0002            -1807000
              1838230179000.0002  -337999.9997558594
                   1838230517000            -5675000
                   1838236192000           -48202000
                   1838284394000            -2097000
              end
              
              format %tcnn/dd/ccYY_hh:MM actiontime
              format %tcHH:MM:SS difference
              
              gen double wanted = actiontime[_n+1] - actiontime 
              
              format %tcHH:MM:SS wanted 
              
              list
              
                   +--------------------------------------+
                   |     actiontime   differ~e     wanted |
                   |--------------------------------------|
                1. |  4/1/2018 4:00   23:00:24   00:59:36 |
                2. |  4/1/2018 5:00   23:28:45   00:31:15 |
                3. |  4/1/2018 5:31   23:51:02   00:08:58 |
                4. |  4/1/2018 5:40   23:35:14   00:24:45 |
                5. |  4/1/2018 6:05   23:05:49   00:54:11 |
                   |--------------------------------------|
                6. |  4/1/2018 6:59   23:29:53   00:30:07 |
                7. |  4/1/2018 7:29   23:54:22   00:05:37 |
                8. |  4/1/2018 7:35   22:25:25   01:34:35 |
                9. |  4/1/2018 9:09   10:36:38   13:23:22 |
               10. | 4/2/2018 10:33   23:25:03          . |
                   +--------------------------------------+

              Comment


              • #8
                Hi Nick Cox ,
                Thanks for your explain , I used your code :

                gen double wanted = actiontime[_n+1] - actiontime I make mistake code for subtract . Thanks

                Comment

                Working...
                X