Convert a character string without separators to time format HH:MM:SS

I have a character string that I know to be of the format HHMMSS (no separators) how do I convert to an unambiguous format such as HH:MM:SS
x <- "005257"
parse_date_time gets me close but I don't want the arbitrary date and timezone.

parse_date_time("005257","HMS")
[1] "0000-01-01 00:52:57 UTC"

would like
"00:52:57"

What kind of object to you want to get out of this process? If you want a string then

gsub("(..)(..)(..)", "\\1:\\2:\\3", "005257")
#> [1] "00:52:57"

Created on 2019-11-13 by the reprex package (v0.3.0.9000)

1 Like

To add on to FJCC's response. You can use the hms package to work with time-of-day variables: https://hms.tidyverse.org/

2 Likes

Another option

library(readr)
x <- "005257"    
parse_time(x, "%H%M%S")
#> 00:52:57
2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.