Announcement

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

  • Remove Seconds from Time

    I'd like to format type double, format %tchh:MM:SS_AM data to be HH:MM AM and HH:MM PM. That is, I'd like to change time data that shows seconds to just show hours and minutes (e.g. 7:59:59 AM to 8:00 AM). When I use the code below, it just cuts off the seconds instead of rounding correctly to the nearest minute.

    Second question is how can I change all "AM" to "PM" in end_time?


    format start_time %tchh:MM_AM

    format end_time %tchh:MM_AM
    replace end_time = subinstr(end_time,"AM","PM",.)

    *Error: "type mismatch"
    Last edited by Kristen Bundy; 14 Nov 2023, 12:07.

  • #2
    PS. I tried

    ceil(start_time)

    and got the error "command ceil is unrecognized"

    Comment


    • #3
      -ceil()- is not a command, it is a function that can be used on the right hand side of the equals sign in a -gen- or -replace- command. In any case, it will not give you what you want, as, used properly, it would round everything up to the next minute. (And as used in #2 it would not actually do anything at all.)

      I think this is what you are after:
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input double time
      42680000
      42695000
      42720000
      end
      format %tchh:MM:SS_AM time
      
      gen double new_time = msofminutes(1)*round(time/msofminutes(1), 1)
      format new_time %tchh:MM_AM
      In the future, when asking for help with coding, it is useful to include example data. And the helpful way to do that is with the -dataex- command, as I have done in this response. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      Comment


      • #4
        This is just to note that the display format here for a date-time does exactly what is conventional, at least in what I see, for dates and times.

        There isn't a roll over or round up until the next interval has started.

        I had a digital watch once that showed the time in form 12:34 and the display changed if and only the watch thought the next minute had started. Same thing is still true on our oven at home, and is surely widespread.

        This is just like the convention for days. It's not tomorrow until tomorrow has started.

        So, as already said, if you want to round, you need to do that with a rounding function. Date-time display formats won't do that, and it's intended behaviour.

        Code:
        . di %tchh:mm clock("12:34:56", "hms")
        12:34
        
        . di %tchh:mm clock("12:34:59", "hms")
        12:34
        
        . di %tchh:mm clock("12:35:00", "hms")
        12:35

        Comment

        Working...
        X