Convert a date vector into Julian day in R Convert a date vector into Julian day in R r r

Convert a date vector into Julian day in R


Try the following to convert from class character(i.e. text) to class POSIXlt, and then extract Julian day (yday):

tmp <- as.POSIXlt("16Jun10", format = "%d%b%y")tmp$yday# [1] 166

For more details on function settings:

?POSIXlt?DateTimeClasses

Another option is to use a Date class, and then use format to extract a julian day (notice that this class define julian days between 1:366, while POSIXlt is 0:365):

tmp <- as.Date("16Jun10", format = "%d%b%y")format(tmp, "%j")# [1] "167"


Similarly:

require(lubridate)x = as.Date('2010-06-10')yday(x)[1] 161

Also note, using lubridate:

> dmy('16Jun10')[1] "2010-06-16 UTC"


You can use R's insol package which has a JD(x, inverse=FALSE) function which converts POSIXct to Julian Day Number (JDN).

insol package also has JDymd(year,month,day,hour=12,minute=0,sec=0) for custom dates.

To display the whole Julian Date (JD) you possibly have to set options(digits=16).