Striping the time out out a date and time vector

I wish to strip the time (hours and minuets) from the following vector but the time seems to defult to 00:00 when I use the following approach.

mytime = "2023-08-27 21:06:00Z"
format(strptime(mytime, "%F"), "%H:%M")

This returns 00:00 but here I want it to return 21:06, i’ve also tried wrapping as.POSIXct() around gtime but had the same results

>Solution :

%F is Equivalent to %Y-%m-%d. So add %H:%M:%S or %T to get also the time.

format(strptime(mytime, "%F %H:%M:%S"), "%H:%M")
#[1] "21:06"

format(strptime(mytime, "%F %T"), "%H:%M")
#[1] "21:06"

Leave a Reply