Search code examples
bashjulian-date

bash - how to get (current) Julian day number?


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.


Solution

  • 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: https://aa.usno.navy.mil/data/JulianDate