简体   繁体   English

strptime返回NA

[英]strptime returning NA

I am getting NA value for my date string using strptime in R. I looked at the various answers, but it didn't work. 我在R中使用strptime获得了日期字符串的NA值。我查看了各种答案,但是没有用。 Here is my code 这是我的代码

startDate=strptime("Wed May 25 01:51:32 UTC 2016", format="%a %B %d %H:%m:%S %Z %Y", tz="UTC")
print(startDate)

Any help would be appreciated. 任何帮助,将不胜感激。

"%H:%m:%S" should be "%H:%M:%S" . "%H:%m:%S"应为"%H:%M:%S" Once you change that, you'll get an error because %Z is not valid for input. 更改后,将收到错误消息,因为%Z对输入无效。

If all the datetime strings have UTC timezone, this will work: 如果所有日期时间字符串都具有UTC时区,则可以使用:

R> strptime("Wed May 25 01:51:32 UTC 2016", "%a %B %d %H:%M:%S UTC %Y", "UTC")
[1] "2016-05-25 01:51:32 UTC"

If not, then you can extract the year and prepend it to the string, because strptime will ignore all characters after those specified by the format string. 如果不是,则可以提取年份并将其添加到字符串前,因为strptime将忽略格式字符串指定的所有字符。

R> dts <- "Wed May 25 01:51:32 UTC 2016"
R> dtf <- "%Y %a %B %d %H:%M:%S"
R> strptime(paste(substring(dts, nchar(dts)-3), dts), dtf, "UTC")
[1] "2016-05-25 01:51:32 UTC"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM