从日期时间中提取时间组件(格式 - HH:MM:SS 和 HH:MM)

extract time component (format - HH:MM:SS and HH:MM) from datetime

如何使用 lubridate 从 datetime 中提取时间组件(格式 - HH:MM:SS 和 HH:MM)?

library(lubridate)
library(dplyr)

readings <- ymd_hms(c("2018-01-05 12:00:15", "2018-01-01 02:00:15", "2018-02-25 12:00:15", 
                  "2018-04-15 11:00:15", "2020-10-15 10:00:15", "2019-10-20 08:00:15",
                  "2019-11-15 02:01:15", "2018-11-02 11:00:15", "2018-07-09 02:00:15",
                  "2020-10-02 01:00:45", "2020-01-29 02:00:55", "2019-03-15 07:00:15")
)

tbl <- tibble(readings)

我们可以用format-

library(dplyr)
library(lubridate)

tbl %>%
  mutate(round_reading = round_date(readings, unit = 'mins'),
         hms = format(round_reading, '%T'), 
         hm = format(round_reading, '%H:%M'))

#   readings            round_reading       hms      hm   
#   <dttm>              <dttm>              <chr>    <chr>
# 1 2018-01-05 12:00:15 2018-01-05 12:00:00 12:00:00 12:00
# 2 2018-01-01 02:00:15 2018-01-01 02:00:00 02:00:00 02:00
# 3 2018-02-25 12:00:15 2018-02-25 12:00:00 12:00:00 12:00
# 4 2018-04-15 11:00:15 2018-04-15 11:00:00 11:00:00 11:00
# 5 2020-10-15 10:00:15 2020-10-15 10:00:00 10:00:00 10:00
# 6 2019-10-20 08:00:15 2019-10-20 08:00:00 08:00:00 08:00
# 7 2019-11-15 02:01:15 2019-11-15 02:01:00 02:01:00 02:01
# 8 2018-11-02 11:00:15 2018-11-02 11:00:00 11:00:00 11:00
# 9 2018-07-09 02:00:15 2018-07-09 02:00:00 02:00:00 02:00
#10 2020-10-02 01:00:45 2020-10-02 01:01:00 01:01:00 01:01
#11 2020-01-29 02:00:55 2020-01-29 02:01:00 02:01:00 02:01
#12 2019-03-15 07:00:15 2019-03-15 07:00:00 07:00:00 07:00