3

I would like to convert a date/times to the Julian Date (number of days since Jan 1 4713 BCE, not the YY-ddd form where ddd is the day number of the current year). This doesn't seem to be built-in to gnudate, but I suspect it's pretty easy with the right incantation of date and bc.

I would prefer to be able to do this from the shell prompt or via a bash function rather than having to install some extra packages. Gnu-date and standard tools like sed awk bc

  • There are many useful variations on this but most sensibly use a scripting language, possibly with good date libraries (look into python), but since you want it with the basics one could hardly do better than to crib from here: blog.sleeplessbeastie.eu/2013/05/17/… – Theophrastus Jun 11 '16 at 16:13
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 
0

If you're only interested in dates that GNU date supports, you can make it spit out the number of seconds since 1970-01-01. Unix time uses a fixed number of seconds per day (leap seconds are smoothed out), so you can convert that number of seconds into a number of days simply by dividing by 86400. To convert that into the Julian day number, add the requisite fixed offset.

echo $(($(TZ=GMT+12 date +%s -d "now") / 86400 + 2440587))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.