简体   繁体   中英

Convert A POSIXt Time To A Julian Day

I was looking for someway to transform a date in POSIXct format to Julian day _ The Julian Day Number (JDN) is the integer assigned to a whole solar day in the Julian day count starting from noon Universal time, with Julian day number 0 assigned to the day starting at noon on Monday, January 1, 4713 BC, proleptic Julian calendar (November 24, 4714 BC, in the proleptic Gregorian calendar) _

But I just figured out how to make a year day - 1 to 365 for leap year. Could someone help me find some function that turns POSIXct dates (like: 2010-10-02 21:00:00) into Julian dates?

I have a column on a dataframe with several dates to be transformed into Julian days.

head(head(all_jub2$timestamp_adjusted)
[1] 2010-10-02 21:00:00 2010-10-03 03:00:00 2010-10-03 09:00:00 2010-10-03 15:00:00
[5] 2010-10-03 21:00:00 2010-10-04 03:00:00
6120 Levels: 2003-10-17 21:00:00 2003-10-18 03:00:00 ... 2020-01-10 09:00:00

You can do this with base R provided you start at the right point . The text in your question your data is still a factor -type variable. That is not good -- you need to parse this, and for example the anytime() function of the anytime package can help you. See other questions here on that.

Back to the question. If you start with a POSIXct variable from current time, and for argument's sake an hour ago to have vector, we can a) convert it to Date and then b) from Date to a Julian:

R> input <- Sys.time() + c(-3600,0)
R> input
[1] "2020-09-29 13:07:50.225898 CDT" "2020-09-29 14:07:50.225898 CDT"
R> as.Date(input)
[1] "2020-09-29" "2020-09-29"
R> julian(as.Date(input))
[1] 18534 18534
attr(,"origin")
[1] "1970-01-01"
R> 

So for today our Julian date is 18534. For your first two data points, we get 14884 and 14885.

R> input <- c("2010-10-02 21:00:00", "2010-10-03 03:00:00")
R> anytime::anydate(input)
[1] "2010-10-02" "2010-10-03"
R> julian(anytime::anydate(input))
[1] 14884 14885
attr(,"tzone")
[1] "America/Chicago"
attr(,"origin")
[1] "1970-01-01"
R> 

If you only want to day of the year, you get that as the component yday of the POSIXlt representation but you need to adjust by one as it is zero based:

R> as.POSIXlt(anytime::anytime(input))$yday + 1
[1] 275 276
R> 

The lubridate package makes it easy to deal with dates. Does this solve your issue?

library(lubridate)

date <- as.POSIXct('2010-10-02 21:00:00')

julian <- yday(date)

Thanks for the answers, but i foun what i've been looking for using the package insol

library(insol)

julian_day <- insol::JD(as.POSIXct('2010-10-02 21:00:00'))
[1] 2455473

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM