Announcement

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

  • how to split datae and time

    dateofvisit 1/25/2020 1:02 1/12/2020 11:55 variable type double. how can i split date and time in to two variable.

  • #2
    See help datetime_functions. I think you want

    gen date = dofc(dateofvisit)
    gen minutes = ms(dateofvisit-date)



    Stata doesn't have a 'time' format, so you can work with minutes or divide by 60 to work with hours.

    hth,
    Jeph

    Comment


    • #3
      The suggestion in #2 needs a tweak.

      You shouldn't subtract the daily date from a clock time even though the difference -- of the order of 20000 (days since 1 Jan 1960) will work out numerically as of the order of 20000 (milliseconds) -- which is almost always immaterial.

      There is no function ms().

      Here is one way to approach it. The time of day in milliseconds since midnight can be assigned a %tc format. If you really want a time in hours, minutes, or seconds since midnight, divide by 60 * 60000, 60000, or 1000 respectively. If you want something else, tell us what it is.

      storm nimbus Please note our request for full real names. https://www.statalist.org/forums/help#realnames

      Code:
      . clear 
      
      . set obs 1 
      number of observations (_N) was 0, now 1
      
      . gen double testexample = clock("1 Sept 2020 09:15", "DMY hm")
      
      . format testexample %tc
      
      . gen date = dofc(testexample)
      
      . format date %td 
      
      . gen double time = mod(testexample, 24 * 60 * 60000)
      
      . format time %tcHH:MM 
      
      . list 
      
           +----------------------------------------+
           |        testexample        date    time |
           |----------------------------------------|
        1. | 01sep2020 09:15:00   01sep2020   09:15 |
           +----------------------------------------+

      Comment


      • #4
        Nick Cox Hardly a 'tweak', but thanks for the correction.

        ps, for -ms()- I intended -mm()-,

        Comment


        • #5
          Nick Cox, I also had a closely related problem. Tried to adapt the suggested solution, but I still can't get it right.

          I have the following data, I would like to separate the date into day, month , year and time.
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double date
          1959013028000
          1959093211000
          1959093236000
          1959093267000
          1959093281000
          1959093327000
          1959093347000
          1959095353000
          1959096940000
          1959096987000
          1959100474000
          1959102275000
          1959102286000
          1959102299000
          1959170897000
          1959170914000
          1959170924000
          1959170931000
          1959191619000
          1959191662000
          1959191681000
          1959191702000
          1959253894000
          1959254807000
          1959254890000
          1959257046000
          1959264797000
          1959264827000
          1959264857000
          end
          format %tc date
          Your help will be greatly appreciated.

          Comment


          • #6
            This follows what is explained at

            Code:
            help datetime

            Code:
            gen day = day(dofc(date))
            gen month = month(dofc(date))
            gen year = year(dofc(date)) 
            gen double time = mod(date, 24 * 60 * 60000)
            format time %tcHH:MM:SS
            There is probably a more direct way to get the time, but the last generate command is I guess doing what you want.

            Comment

            Working...
            X