Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert POSIX date to day of year?

Tags:

datetime

r

The title has it: how do you convert a POSIX date to day-of-year?

like image 546
Gregor Thomas Avatar asked Oct 31 '11 18:10

Gregor Thomas


People also ask

What is Posix date format?

POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970. Negative numbers are used to store dates prior to 1970. Thus, the POSIXct format stores each date and time a single value in units of seconds. Storing the data this way, optimizes use in data.

What is the difference between POSIXct POSIXlt and as date?

The builtin as. Date function handles dates (without times); the contributed library chron handles dates and times, but does not control for time zones; and the POSIXct and POSIXlt classes allow for dates and times with control for time zones.

How do I get the year from a date in R?

To get the year from a date in R you can use the functions as. POSIXct() and format() . For example, here's how to extract the year from a date: 1) date <- as. POSIXct("02/03/2014 10:41:00", format = "%m/%d/%Y %H:%M:%S) , and 2) format(date, format="%Y") .


1 Answers

An alternative is to format the "POSIXt" object using strftime():

R> today <- Sys.time() R> today [1] "2012-10-19 19:12:04 BST" R> doy <- strftime(today, format = "%j") R> doy [1] "293" R> as.numeric(doy) [1] 293 

which is preferable to remembering that the day of the years is zero-based in the POSIX standard.

like image 193
Gavin Simpson Avatar answered Oct 19 '22 04:10

Gavin Simpson