Convert Day of Year to Datetime

Hi Everyone,

I'm currently trying to convert a day of the year (DOY) value with decimals to datetimes. What I mean by this for example, the value 32.5 represents the date Feburary 1st, 12pm UTC. I've looked all over and I can't seem to find anything to convert this properly. Any suggestions would be great.

An example dataframe for code

df<-data.frame("DOY" = c(32.5, 33, 33.5), "Value" = c(5,10,7))

How about this?

# 1.4 days into the year
secs_per_day <- 24*3600
as.POSIXct(1.4*secs_per_day,tz="GMT",origin="2019-12-31")
[1] "2020-01-01 09:36:00 GMT"
1 Like

Here is a similar approach using lubridate functions. If you want 1.0 to mean Jan 01 00:00:00, then I guess you have to subtract 1 from the day-of the-year value.

DATE <- ymd_hms("2020-01-01 00:00:00")
DATE %m+% seconds_to_period(32.5*86400)
[1] "2020-02-02 12:00:00 UTC"
1 Like

This is exactly what I'm looking for. Thank you!

This is also a great solution (I do like to use lubridate whenever possible). I also found that using the other solution above and setting the origin to one day earlier also solves the issue of making January 1st the first day.

This topic was automatically closed 7 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.