R lubridate converting seconds to date

I use the lubridate solution provided by @leerssej

But in case anyone prefers @IRTFM's solution in base R, but also wants minutes and seconds, here's an example of how to do that:

as.POSIXct("2019-03-15 16:17:42" , format="%Y-%m-%d %H:%M:%OS")

lubridate has an as_datetime() that happens to have UNIX epoch time as the default origin time to make this really simple:

> as_datetime(1356129107)
[1] "2012-12-21 22:31:47 UTC"

more details can be found here: https://rdrr.io/cran/lubridate/man/as_date.html


Dirk is correct. However, if you are intent on using lubridate functions:

paste( year(dt), month(dt), mday(dt), hour(dt) sep="-")

If on the other hand you want to handle the POSIXct objects the way they were supposed to be used then this should satisfy:

format(x, format="%Y-%m-%d-%H")

Tags:

R

Lubridate