as.POSIXct with datetimes including midnight

前端 未结 4 1814
醉话见心
醉话见心 2020-12-12 00:47

I am wanting to convert date-times stored as characters to date-time objects. However if a date time includes midnight then the resulting datetime object excludes the time c

4条回答
  •  没有蜡笔的小新
    2020-12-12 01:24

    lubridate::as_datetime() is more flexible, accepting both dates (interpreted as 00:00:00) and datetimes.

    example.dates <- c("2011-11-02", "2011-11-02 00:31:00","2011-11-02 00:00:00","2011-11-02 00:20:22")
    library(lubridate)
    
    ymd_hms(example.dates)
    #> Warning: 1 failed to parse.
    #> [1] NA                        "2011-11-02 00:31:00 UTC"
    #> [3] "2011-11-02 00:00:00 UTC" "2011-11-02 00:20:22 UTC"
    
    as_datetime(example.dates)
    #> [1] "2011-11-02 00:00:00 UTC" "2011-11-02 00:31:00 UTC"
    #> [3] "2011-11-02 00:00:00 UTC" "2011-11-02 00:20:22 UTC"
    

提交回复
热议问题