Announcement

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

  • Epoch & Unix Timestamp

    Hello, i need assistance in converting Unix time stata to a Human readable format.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double time_stamp
    1466425303
    1466425324
    1466425339
    1466425487
    1466425488
    1466425513
    1466425535
    1466425578
    1466425588
    1466425600
    1466425619
    1466425622
    end
    I am using this format format time_stamp %tcDayname,Mon_DD,ccYY_HH:MM:SS_am but its giving inaccurate results.
    I am expecting something similar to this: Monday, June 20, 2016 3:25:13 PM

  • #2
    See

    Code:
    help datetime
    where the solution is documented for you.


    Code:
    clear
    input double time_stamp
    1466425303
    1466425324
    1466425339
    1466425487
    1466425488
    1466425513
    1466425535
    1466425578
    1466425588
    1466425600
    1466425619
    1466425622
    end
    
    rename time_stamp unixtime 
    generate double statatime = unixtime * 1000 + mdyhms(1,1,1970,0,0,0)
    
    format unixtime %10.0f 
    format statatime %tc 
    list 
    
         +---------------------------------+
         |   unixtime            statatime |
         |---------------------------------|
      1. | 1466425303   20jun2016 12:21:43 |
      2. | 1466425324   20jun2016 12:22:04 |
      3. | 1466425339   20jun2016 12:22:19 |
      4. | 1466425487   20jun2016 12:24:47 |
      5. | 1466425488   20jun2016 12:24:48 |
         |---------------------------------|
      6. | 1466425513   20jun2016 12:25:13 |
      7. | 1466425535   20jun2016 12:25:35 |
      8. | 1466425578   20jun2016 12:26:18 |
      9. | 1466425588   20jun2016 12:26:28 |
     10. | 1466425600   20jun2016 12:26:40 |
         |---------------------------------|
     11. | 1466425619   20jun2016 12:26:59 |
     12. | 1466425622   20jun2016 12:27:02 |
         +---------------------------------+

    Comment


    • #3
      Thank you so much Cox, very appreciative

      Comment


      • #4
        Also reminded that, if we want to extract "year" from the time-format data (i.e., %tc), we can type
        gen year = yofd(dofc(statatime)).
        For more details, see: help datetime.

        Comment

        Working...
        X