Announcement

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

  • Convert time with a 12 hrs setting, to 24 hrs

    Dear all,

    I have to convert a variable setting in "12 hours" to "24 hours". I divided the variable into v2= "time" and v3="AM/PM", and then added 12hours if v3 == "PM", but this creates a problem for the 12PM, that are converted to midnight.

    The code:
    Click image for larger version

Name:	code.png
Views:	1
Size:	10.6 KB
ID:	1655285


    The output:
    Click image for larger version

Name:	bro.png
Views:	1
Size:	5.2 KB
ID:	1655286


    I'd be grateful for any advice, thank you in advance,

    Aurora

  • #2
    You do not want to add 12 hours to 12PM: hours 0-11 are AM and hours 12-23 are PM.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str8 v2 str2 v3
    "12:40:49" "PM"
    "1:20:42"  "PM"
    end
    
    gen double time_part = clock(v2, "hms")
    format time_part %tcHH:MM:SS
    replace time_part = time_part + 12*60*60*1000 if v3=="PM" & hh(time_part)<12
    list, clean abbreviate(12)
    Code:
    . list, clean abbreviate(12)
    
                 v2   v3   time_part  
      1.   12:40:49   PM    12:40:49  
      2.    1:20:42   PM    13:20:42

    Comment


    • #3
      I have seen the equivalent of "12:30" or "24:30" in raw data for times in the first hour after midnight, sometimes systematically so.

      The first is, in effect, what my watch tells me too.

      An ad hoc fix to the data is I think the only serious option. .

      Comment

      Working...
      X