com*_*ous 6 datetime r lubridate
我正在尝试使用 lubridate 包创建日期时间,并且我尝试了以下值:
library(lubridate)
ymd_hms("2017-07-02 23:00:00")
[1] "2017-07-02 23:00:00 UTC"
ymd_hms("2017-07-02 00:00:00")
[1] "2017-07-02 UTC"
Run Code Online (Sandbox Code Playgroud)
看起来,如果您输入午夜时间 00:00:00,ymd_hms() 方法会忽略时间值,结果会变成日期。有什么办法可以在结果中显示 00:00:00 吗?多谢!
所以在谷歌搜索和错误尝试之后我刚刚得到了这个问题的答案:
mydates <- format(as.POSIXct("2011-01-01 00:00:00", tz = "UTC"), "%m-%d-%Y %H:%M:%S")
mydates
[1] "01-01-2011 00:00:00"
format(as.POSIXct("2011-01-01 00:00:00", tz = "UTC"), "%m-%d-%Y %H:%M:%S")
[1] "01-01-2011 00:00:00"
Run Code Online (Sandbox Code Playgroud)
看来使用带有格式参数的 R Base as.POSIXct 是有效的。