How to get today's Julian day number (JDN) equivalent? or any date's, for that matter?
I've looked and looked, but only found some functions that produced "year-dayOfYear", not something like: 2457854.
How to get today's Julian day number (JDN) equivalent? or any date's, for that matter?
I've looked and looked, but only found some functions that produced "year-dayOfYear", not something like: 2457854.
in bash date +%j
returns julian date.
[root@TX-Serv-1 ~]# date +%j
108
Also Julian dates in the future are pretty easy too.
[root@es-host01 ~]# date --date='10 days' +%j
119
[root@es-host01 ~]# date --date='2 weeks' +%j
123
I ended up porting one myself. Here is a SSCCE:
#!/bin/bash
function GetJulianDay # year, month, day
{
year=$1
month=$2
day=$3
jd=$((day - 32075 + 1461 * (year + 4800 - (14 - month) / 12) / 4 + 367 * (month - 2 + ((14 - month) / 12) * 12) / 12 - 3 * ((year + 4900 - (14 - month) / 12) / 100) / 4))
echo $jd
}
function GetTodayJulianDay
{
year=`date +"%Y"`
month=`date +"%m"`
day=`date +"%d"`
todayJd=$(GetJulianDay $year $month $day)
echo $todayJd
}
# samples
todayJd=$(GetTodayJulianDay)
echo "got today jd: $todayJd"
yesterdayJd=$(GetJulianDay 2017 4 9)
echo "got yesterday jd: $yesterdayJd"
You could get the Unix date with date +%s --date=YYYYMMDD
and use it to compute the Julian Day from Unix date
It would end up something like:
echo $(( $(date +%s --date=YYYYMMDD) / 86400 + 2440587 ))
Edit: I don’t know how important this is, but some people like to add 0.5 to the conversion. Basically this means that if your current date/time is after noon, the conversion would put the Julian date 1 day later.
OLD_JULIAN_VAR=$(date -u -d 1840-12-31 +%s)
TODAY_DATE=`date --date="$odate" +"%Y-%m-%d"`
TODAY_DATE_VAR=`date -u -d "$TODAY_DATE" +"%s"`
export JULIAN_DATE=$((((TODAY_DATE_VAR - OLD_JULIAN_VAR))/86400))
echo $JULIAN_DATE
the mathematically
[(date in sec)-(1840-12-31 in sec)]/86400
To fetch Julian Date of any specific day
RUN_DT=$(date -d "20230715" "+%Y/%m/%d")
JULIAN_DT=$(date -d $RUN_DT +%Y%j)
echo "$JULIAN_DT"
Output: 2023196
To fetch Julian date for First day of previous month?
RUN_DT=$(date -d "20230715 - 1 month" "+%Y/%m/01")
JULIAN_DT=$(date -d $RUN_DT +%Y%j)
echo "$JULIAN_DT"
Output: 2023152
To fetch Today's Date Julian date
REQUEST_DATE=$(date +%Y%j)
Output for 30 Aug 2023: 2023242