解析压缩日期时间年份和小时之间没有分隔字符

问题描述:

我已经尝试了几种不同的方法解析日期时间R在这个日期时间,lubridate :: mdy_hm似乎工作,但也有一个奇怪的行为,不处理单个数组的元素?解析压缩日期时间年份和小时之间没有分隔字符

datetimes <- c("10/6/20176:00 PM EDT", "10/16/20171:00 PM EDT", "10/6/201711:00 PM EDT", "10/16/201711:00 PM EDT") 

substrRight <- function(x, n){ 
    substr(x, nchar(x)-n+1, nchar(x)) 
} 

(time_with_bad_nas <- substrRight(datetimes, 11)) # two should be 11:00 PM ET 
(date_with_bad_nas <- substr(datetimes, 0, 10)) # two are capturing the hour in the year 

lubridate::mdy_hm(datetimes[1], tz = "America/New_York") 
lubridate::mdy_hm(datetimes, tz = "America/New_York") 

datetimes[1] == "10/6/20176:00 PM ET" 
+0

'(\ d +)\ /(\ d +)\ /(\ d {4})(\ d +) :(\ d +)\ s +([ap] m)\ s +([az] +)'见[这里](https://regex101.com/r/2WWKvv/3)获取每个部分。如果您需要可能获得2年格式,请使用:(\ d +)\ /(\ d +)\ /(\ d {4} | \ d {2})(\ d +):(\ d +)\ s +( [ap] m)\ s +([az] +)':https://regex101.com/r/2WWKvv/2 – ctwheels

人们可以尝试:

as.POSIXct(datetimes, format = "%m/%d/%Y%I:%M %p", tz = "America/New_York") 

输出:

"2017-10-06 18:00:00 EDT" "2017-10-16 13:00:00 EDT" "2017-10-06 23:00:00 EDT" "2017-10-16 23:00:00 EDT"