How to create date from datetime (using lubridate)?

前端 未结 2 2011
别那么骄傲
别那么骄傲 2021-01-12 08:14

Assume I have created a variable containing date and time:

a <- ymd_hms(\"2014-01-01 12:23:34\")

How do I create another variable that o

相关标签:
2条回答
  • 2021-01-12 08:58

    Using date() is much shorter and avoids any issues with rounding.

    library(lubridate)
    date(a)
    [1] "2014-01-01"
    
    0 讨论(0)
  • 2021-01-12 09:07

    You can just use round_date

    round_date(a, "day")
    [1] "2014-01-02 UTC"
    

    EDIT

    You do need to be careful with rounding the time though. For complete equivalence here you would need to use floor_date.

    identical(ymd("2014-01-01"), floor_date(a, "day"))
    [1] TRUE
    
    0 讨论(0)
提交回复
热议问题