Announcement

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

  • Extracting time from text string

    Hi all,

    I have text strings in variable RideDate that look like this:

    2020/11/02, 16:56:12
    2020/11/02, 16:46:33
    2020/11/02, 13:57:04
    2020/11/02, 13:56:56
    With some even in this format too...
    2020/11/07, 12:41:02 PM
    2020/11/07, 12:33:24 PM
    2020/11/07, 12:18:36 PM
    2020/11/07, 12:14:33 PM

    I need to extract the timestamp from this text string.

    I have used this code to extract the date from the same variable:
    >> gen Date=date(RideDate,"YMD###")
    >> format Date %td

    Thank you!

  • #2
    Consider this:

    Code:
    di %tc clock("2020/11/02, 16:56:12", "YMDhms")
    di %tcHH:MM clock("2020/11/02, 16:56:12", "YMDhms")
    di %tc clock("2020/11/07, 12:41:02 PM", "YMDhms")
    di %tcHH:MM clock("2020/11/07, 12:41:02 PM", "YMDhms")
    di %tc clock("2020/11/07, 12:41:02 AM", "YMDhms")
    di %tcHH:MM clock("2020/11/07, 12:41:02 AM", "YMDhms")

    Res.:

    Code:
    . di %tc clock("2020/11/02, 16:56:12", "YMDhms")
    02nov2020 16:56:12
    
    . 
    . di %tcHH:MM clock("2020/11/02, 16:56:12", "YMDhms")
    16:56
    
    . 
    . di %tc clock("2020/11/07, 12:41:02 PM", "YMDhms")
    07nov2020 12:41:02
    
    . 
    . di %tcHH:MM clock("2020/11/07, 12:41:02 PM", "YMDhms")
    12:41
    
    . 
    . di %tc clock("2020/11/07, 12:41:02 AM", "YMDhms")
    07nov2020 00:41:02
    
    . 
    . di %tcHH:MM clock("2020/11/07, 12:41:02 AM", "YMDhms")
    00:41

    Note that time variables should always be stored as double. For more, see

    Code:
    help datetime
    Last edited by Andrew Musau; 13 Nov 2020, 07:59.

    Comment


    • #3
      Thanks Andrew but I have 12,000+ observations all with a unique time and date. Is there perhaps another way that will apply to all observations?

      Comment


      • #4
        Andrew's post demonstrates the use of the clock() function to convert strings to Stata Internal Format date-and-time values. Replace the date() function and the %td format in your code in post #1 with suitable uses of the clock() function and %tc format in your code.

        Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

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

        Comment


        • #5
          Code:
          gen double wanted = clock(RideDate, "YMDhms")
          format wanted %tcHH:MM
          would be the way to adapt #2.

          Comment

          Working...
          X