Announcement

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

  • How to round time to days?

    I have a variable of datetime. For instance:

    Code:
    1/3/15 12:23:33
    3/5/15 15:31:01
    5/2?15 01:01:2
    I want to round it in two ways. First, how do I round it to 00:00:00? For instance, I want the first row to become "1/3/15 00:00:00." Second, how do I round it up to the last second of the day? For instance, I want the first row to become "1/3/15 23:59:59." Sorry for not using -dataex-. I tried it, but it wouldn't spit out the values in %tc format.


  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double time
    1735907013000
    1741188661000
    1746147680000
    end
    format %tc time
    
    gen double round_down = cofd(dofc(time))
    gen double round_up = cofd(dofc(time) + 1) - 1000
    format round_* %tc
    
    list, noobs clean
    Notes:

    1. As you can see from the above, -dataex- specifically does not put out something that human eyes will recognize as dates and times. It's not supposed to. It puts out the double-precision numbers that Stata uses internally, and then a command to apply %tc formatting to them.

    2. The third observation in your example is not a valid date-time. I assume you made some typos in creating the example, and I made a possible correction.

    Comment


    • #3
      Thanks!

      Comment

      Working...
      X