R lubridate converting seconds to date
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")
how to convert seconds to a date in R
Convert to POSIXct and add the number of seconds. seconds
can be a vector of seconds.
seconds <- 2
as.POSIXct("09/01/2017 01:37:33", format = "%m/%d/%Y %H:%M:%S") + seconds
## [1] "2017-09-01 01:37:35 EDT"
Converting seconds from specific time on a specific date in R
Have a look at lubridate
...
library(lubridate)
ymd_hm("2017-05-21 22:00") + seconds(1.01)
So in your case it would be something like
file$date <- ymd_hm("2017-05-21 22:00") + seconds(file$Time)
Convert seconds to days: hours:minutes:seconds
You may try
library(lubridate)
seconds_to_period(86400)
#[1] "1d 0H 0M 0S"
seconds_to_period(48000)
#[1] "13H 20M 0S"
If you need to format
td <- seconds_to_period(86400)
sprintf('%02d %02d:%02d:%02d', day(td), td@hour, minute(td), second(td))
#[1] "01 00:00:00"
If it spans for >99
days,
td <- seconds_to_period(1e7)
sprintf('%03d %02d:%02d:%02d', day(td), td@hour, minute(td), second(td))
#[1] "115 17:46:40"
Convert numeric time in milliseconds to datetime with seconds with decimal value
With lubridate
, using as_datetime
library(lubridate)
as_datetime(time/1000)
[1] "2021-10-08 16:01:17 UTC"
Note that the milliseconds are not print
ed in the console. If we need to print, then format with strftime
or format
(but it will not be a datetime object anymore)
strftime(as_datetime(time/1000), '%Y-%m-%d %H:%M:%OS3')
#[1] "2021-10-08 11:01:17.772"
Or without using any package, just specify it in as.POSIXct
as.POSIXct(time/1000, origin = '1970-01-01')
[1] "2021-10-08 11:01:17 CDT"
Convert UNIX epoch to Date object
Go via POSIXct
and you want to set a TZ
there -- here you see my (Chicago) default:
R> val <- 1352068320
R> as.POSIXct(val, origin="1970-01-01")
[1] "2012-11-04 22:32:00 CST"
R> as.Date(as.POSIXct(val, origin="1970-01-01"))
[1] "2012-11-05"
R>
Edit: A few years later, we can now use the anytime package:
R> library(anytime)
R> anytime(1352068320)
[1] "2012-11-04 16:32:00 CST"
R> anydate(1352068320)
[1] "2012-11-04"
R>
Note how all this works without any format or origin arguments.
Related Topics
How to Call the 'Function' Function
Clustered Standard Errors in R Using Plm (With Fixed Effects)
Change Color Actionbutton Shiny R
Apply Tidyr::Separate Over Multiple Columns
Use Dygraph for R to Plot Xts Time Series by Year Only
How to Prevent Objects from Automatically Loading When I Open Rstudio
Offline Installation of R Packages
Ddply Multiple Quantiles by Group
Lme4::Glmer VS. Stata's Melogit Command
Saving a List of Plots by Their Names()
R How to Change One of the Level to Na
Using Strsplit and Subset in Dplyr and Mutate
Formatting Number Output of Sliderinput in Shiny
R: How to Select Files in Directory Which Satisfy Conditions Both on the Beginning and End of Name