将char转换为POSIXct

时间:2018-04-09 13:16:14

标签: r

这是我的转换日期: " 16:00:00 CT 08年4月8日"

这是我的尝试:

x <- "16:00:00 CT 08 Apr 2018"
Sys.setlocale(category = "LC_ALL", locale = "English_United States.1252")
as.POSIXct(x, format = '%H:%M:%S %A %d %b %Y')

并返回NA

2 个答案:

答案 0 :(得分:4)

我们可以在CT

中使用format
as.POSIXct(x, format = '%H:%M:%S CT %d %b %Y')

答案 1 :(得分:1)

删除字符串CT并继续前进:

as.POSIXct(paste(unlist(strsplit(x," CT ")),collapse = ""),format='%H:%M:%S %d %b %Y')
[1] "2018-04-08 16:00:00 GMT"