使用 lubridate 将带有月份缩写的日期时间字符串转换为日期时间对象

convert datetime string with month abbreviation to datetime object using lubridate

如何使用 lubridate 将此字符串转换为日期时间?它有月份的缩写。 期望的输出是 12/1/2020 9:19:00 AM

library(lubridate)

datetime_string = "01-DEC-2020 09:19 AM"

试试这个:

datetime_string = "01-DEC-2020 09:19 AM"
timeValue <- lubridate::dmy_hm(datetime_string) 

format(timeValue, "%m/%d/%Y %H:%M:%S %p")
# "12/01/2020 09:19:00 AM"

使用base R

format(as.POSIXct(datetime_string, format = '%d-%b-%Y %H:%M %p'), '%m/%d/%Y %H:%M:%S %p')
[1] "12/01/2020 09:19:00 am"