问题
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.
回答1:
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
回答2:
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"
Test it: http://aa.usno.navy.mil/data/docs/JulianDate.php
回答3:
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.
回答4:
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
来源:https://stackoverflow.com/questions/43317428/bash-how-to-get-current-julian-day-number