Convert total minutes (since the beginning of the year) into date

Hi,
i have a vector like: 1823, 2674, 6129 and so on. Each number represents the minutes since the beginning of the year. How do I extract the date (day/month/year hour:minute)? Let it be the year 2016 (leap year) Thank you.

Daniele

Hi Daniele

This can simply be done by using the as.Date function where you can provide an origin date. I wrote a quick example:


calculateDate <- function(year, minutes){

# here the 1440 is number of minutes in a day.  as the function needs that value in days.   
z <- as.Date(minutes/1440, origin = paste0(year,"-01-01"))
  
  ## covert to POSIXct date to show the time also, otherwise these wont show
  z <- as.POSIXct.Date(z)
  print(z)
}

this would output:

> calculateDate(2016, 16543)
[1] "2016-01-12 12:43:00 CET"

Hope this helps.

Best,
H

Another method using lubridate.

library(lubridate)
DATE <- ymd_hms("2016-01-01 00:00:00")
DATE  %m+%  minutes(c(1823, 2674, 6129))
[1] "2016-01-02 06:23:00 UTC" "2016-01-02 20:34:00 UTC" "2016-01-05 06:09:00 UTC"
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.