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

Buffalo picture Buffalo · Apr 10, 2017 · Viewed 8.5k times · Source

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.

Answer

Buffalo picture Buffalo · Apr 10, 2017

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