date calc

Hi,
I need subtract two date values (which are in day of the year format) and the output would give the remaining days. using the command date +"%j" i would get today's 'day of the year' i.e.,

> date +"%j"
256

Next, i need to take input of a previous date in the format 09/05/2012 and then convert it to 'day of the year format'. Could anyone please help how i could do this?
Note: I'm using HP-UX

Do you have GNU date? if yes you can refer its man page.. if not you have to rely on good old days of date logic convert them to minutes and do minus then convert back to number of days or years so...

See if you find this useful: days elapsed between 2 dates

#/bin/ksh

# date format: mm/dd/yyyy

typeset -Z -R2 mm dd

y=${1##*/}
m=${1%%/*}
d=${1#*/}
d=${d%/*}
mm=$m
dd=$d
date="$mm/$dd/$y"

doy=0
f=0;
for mm in 1 2 3 4 5 6 7 8 9 10 11 12
do
   cal $mm $y | while read line
   do
      line=${line%%[a-zA-Z]*}
      [[ -n ${line:-""} ]] && {
         for dd in $line
         do
            (( doy = doy + 1 ))
            [[ "$mm/$dd/$y" = "$date" ]] && { f=1 ; break ; }
         done
         [[ "$mm/$dd/$y" = "$date" ]] && { break ; }
      }
   done
   [[ "$mm/$dd/$y" = "$date" ]] && { break ; }
done

[[ $f = 0 ]] && doy="Date not found"
echo $doy

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums