Announcement

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

  • How can I solve date and time issues

    Hello
    In my data set ,

    When I subtract case : 4/1/2018 5:00 - 4/1/2018 4:00 =12:59:36 ( format in %tchh:mm:ss) in back ground : 01jan1960 00:59:36 , but really the answer is 00:59:36 so how can I solve the correct answer :00:59:36 .Please help me with this !



  • #2
    Stata date/time storage is the number of milliseconds from 1/1/1960 00:00:00. When you subtract two date/times and format it as a date/time, then you will still be showing the number of milliseconds from 1/1/1960 00:00:00. What you want is elapsed time between two date/times.

    The -egenmore- package available from SSC (type: ssc install egenmore) has a function -elap()- that will calculate the number of seconds between two time points and format it in the way you wish (in a string variable):

    Code:
    ssc install egenmore
    
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str17 var1 str16 var2
    "4/1/2018 5:00:21"  "4/1/2018 4:00:13"
    "4/1/2018 9:02:10"  "3/31/2018 9:00:10"
    end
    gen double t1=clock(var1, "MDYhms")
    format t1 %tchh:mm:ss
    gen double t2=clock(var2, "MDYhms")
    format t2 %tchh:mm:ss
    
    gen dif=t1-t2  //subtract two date/times
    gen dif_sec=dif/1000  //milliseconds to seconds
    egen wanted=elap(dif_sec)
    list
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      I agree with @Carole Wilson. If you are interested in durations (differences between times) measured in seconds, then use a difference variable that is also in seconds.

      Comment


      • #4
        Thanks for all of you ..

        Comment

        Working...
        X