3

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.

Buffalo
  • 3,861
  • 8
  • 44
  • 69

5 Answers5

6

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
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 2
    _How_ does "108" resemble anything like a JDN? please read the question thoroughly – Buffalo Apr 19 '17 at 14:24
  • While it's answering the wrong question (ordinal date/day-of-year number instead of Julian date), this is a helpful little command. Here's the difference between the two, which shows why there could be confusion if you only read the question's title: https://en.wikipedia.org/wiki/Julian_day#Terminology – kevinmicke Apr 24 '17 at 20:42
3

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

Buffalo
  • 3,861
  • 8
  • 44
  • 69
2

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.

Community
  • 1
  • 1
vdavid
  • 2,434
  • 1
  • 14
  • 15
  • I went with the tried and tested Java version we'd been using. Yours looks much neater. I'll give it a chance when I have some time. – Buffalo Apr 19 '17 at 14:33
  • For my bash function, I used `local epoch_seconds=$(date -j -f %Y-%m-%d-%H:%M:%S $year-$month-$day-12:00:00 +%s)`, then `local epoch_days=$(( epoch_seconds / (60*60*24) ))`, and finally `local jd=$(( epoch_days + 2440588 ))`. Depending on need, may have to be mindful of local time versus UTC. – Eljay Oct 18 '22 at 16:22
0
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 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

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

Mohit Bagaria
  • 21
  • 1
  • 2