Converting YYYYMMDD to Julian

I am writing some PERL code (and I realize this is a UNIX forum), but was wondering if anyone has a quick routine (PERL or shell scripting) to take a date in YYYYMMDD format and return the 3 digit Julian number.

For instance, my program will have a variable called "$Settlement_Date" and will contain a value like this: "20050422", and I would like it to return a new variable with the value "112".

Any help or insight would be appreciated.

Sorry, actually need to TAKE a 3 digit julian and convert it to YYYYMMDD format. Original post was stated incorrectly.

something to start with:

#!/bin/ksh
#-----------------------------------------------------------------------------
# Date manipulation routines - convering to Julian and back
#
# Julian Day Number from calendar date
date2julian() #  year month day
{
  typeset year=$1;  typeset month=$2;  typeset day=$3
  tmpmonth=$((12 * year + month - 3))
  tmpyear=$((tmpmonth / 12))
  print $(( (734 * tmpmonth + 15) / 24 -  2 * tmpyear + \
    tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))
}

# Calendar date from Julian Day Number
julian2date() # julianday
{
  #set -x
  typeset tmpday=$(($1 - 1721119))
  typeset centuries=$(( (4 * tmpday - 1) / 146097))
  tmpday=$((tmpday + centuries - centuries/4))
  typeset year=$(( (4 * tmpday - 1) / 1461))
  tmpday=$((tmpday - (1461 * year) / 4))
  typeset month=$(( (10 * tmpday - 5) / 306))
  day=$((tmpday - (306 * month + 5) / 10))
  month=$((month + 2))
  year=$((year + month/12))
  month=$((month % 12 + 1))
  printf "%s %02d %02d" $year $month $day
}

# Day of week, Monday=1...Sunday=7
dow() # year month day
{
  print $(( $(date2julian $1 $2 $3) % 7 + 1))
}
1 Like

I'm probably just being stupid, but what am I doing wrong? Here is the code and the result I am getting out. Trying to pass in Julian Date of 112 to have it return 20050422...

#! /usr/bin/ksh

# Calendar date from Julian Day Number
julian2date()
{
#set -x
typeset tmpday=$(($1 - 1721119))
typeset centuries=$(( (4 * tmpday - 1) / 146097))
tmpday=$((tmpday + centuries - centuries/4))
typeset year=$(( (4 * tmpday - 1) / 1461))
tmpday=$((tmpday - (1461 * year) / 4))
typeset month=$(( (10 * tmpday - 5) / 306))
day=$((tmpday - (306 * month + 5) / 10))
month=$((month + 2))
year=$((year + month/12))
month=$((month % 12 + 1))
printf "%s %02d %02d" $year $month $day
}

julian2date 112

RESULT:

/home/rsd9/xxgvlqr> test.ksh
-4711 -8 -15/home/rsd9/xxgvlqr>

Date arithmetic is covered in our faq section. You might want to look at that.

this seems to work ok for me - you might want to look into FAQ as well [as suggested by Perderabo:

#!/bin/ksh

# Calendar date from Julian Day Number
julian2date()
{
typeset tmpday=$(($1 - 1721119))
typeset centuries=$(( (4 * tmpday - 1) / 146097))
tmpday=$((tmpday + centuries - centuries/4))
typeset year=$(( (4 * tmpday - 1) / 1461))
tmpday=$((tmpday - (1461 * year) / 4))
typeset month=$(( (10 * tmpday - 5) / 306))
day=$((tmpday - (306 * month + 5) / 10))
month=$((month + 2))
year=$((year + month/12))
month=$((month % 12 + 1))
printf "%s %02d %02d" $year $month $day
}

date2julian() #  year month day
{
  typeset year=$1;  typeset month=$2;  typeset day=$3
  typeset tmpmonth=$((12 * year + month - 3))
  typeset tmpyear=$((tmpmonth / 12))
  print $(( (734 * tmpmonth + 15) / 24 -  2 * tmpyear + \
  tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))
}


printf "today->[2005 4 29] julian->[%d]\n" $(date2julian 2005 4 29)
printf "and back from julian->[%s %s %s]\n" $(julian2date $(date2julian 2005 4 29))