Announcement

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

  • How to separate time and date

    Dear all

    I would like to separate time and date and have the final output in numeric form.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str26 q2_start_time
    "2016-11-08T10:39:38.643+03"
    "2016-11-08T11:40:12.518+03"
    "2016-11-08T11:41:36.654+03"
    "2016-11-09T09:24:14.477+03"
    "2016-11-09T10:11:58.300+03"
    "2016-11-09T10:50:00.873+03"
    "2016-11-09T11:19:39.984+03"
    "2016-11-09T11:49:55.398+03"
    "2016-11-09T12:15:10.411+03"
    "2016-11-09T12:53:27.624+03"
    end

    Thanks a lot for your help.

  • #2
    This should help, except that

    1. Your example doesn't make clear whether your dates are YMD or YDM (November 8, etc. or 11 August, etc) but from the sequence I guess the first.

    2. I am probably missing something obvious but I don't know for certain what "+03" means. Perhaps a time zone indicator?

    Code:
    gen ddate = daily(substr(q2_start_time, 1, 10), "YMD")
    format ddate %td 
    
    gen double ctime = clock(substr(q2_start_time, 12, 12), "hms") 
    format ctime %tcHH:MM:SS.sss
    
    list ????? 
    
         +--------------------------+
         |     ddate          ctime |
         |--------------------------|
      1. | 08nov2016   10:39:38.643 |
      2. | 08nov2016   11:40:12.518 |
      3. | 08nov2016   11:41:36.654 |
      4. | 09nov2016   09:24:14.477 |
      5. | 09nov2016   10:11:58.300 |
         |--------------------------|
      6. | 09nov2016   10:50:00.873 |
      7. | 09nov2016   11:19:39.984 |
      8. | 09nov2016   11:49:55.398 |
      9. | 09nov2016   12:15:10.411 |
     10. | 09nov2016   12:53:27.624 |
         +--------------------------+
    This is all documented under help datetime etc. You don't need to do it this way, as you could extract date-time as one, then split into components.

    Comment


    • #3
      Thanks a lot Nick for your kind help.

      Comment

      Working...
      X