Hi:
First, I don't think you can solve this problem with just the date command unless you take the first suggestion. Second, I'd use perl to solve this problem. If you don't have perl, but you have access to GNU's awk, gawk, the following script provides yesterday's
date:
time.sh -1
You'll have to change the gawk internal strftime function formatting to fit your own fromatting needs.
Go to http//
for a copy of gawk.
#!/bin/bash
# time.sh
# Author: Julian Cates
#
# Figure what a date was/will be, given an offset from current day:
#./time.sh -7
# Thu May 17 10:49:06 CDT 2001
#======================================================================
ME=`basename ${0}`
if [ $# -ne 1 ]
then echo "Usage is $ME [+/-] # of days"
else
gawk -v days=$1 'BEGIN {
target_date = systime()+(86400*days)
datestring = strftime("%c", target_date)
printf "%s\n\n",datestring }'
fi
Third, if you don't want to use the GNU tools, then you need to do some programming. I choose to manipulate the Julian Date. Not the modern julian date which counts the number of days from January 1 of the current year, but the astronomer's algorithm that tracks days from Jan. 1, 4713 BC.
There are well known algorithms that are easily implemented. The following ksh script gets the Julian Date of the system date, subtracts 1 to get yesterday's JD, and
converts yesterday's JD to the gregorian date:
#!/bin/ksh
# The Julian date (JD) is a continuous count of days from 1 January 4713 BC. The following
# algorithm is good from years 1801 to 2099
# See URL:
for more information
get_JD ()
{
typeset -i JDD
JDD=$(($1-32075+1461*($3+4800+($2-14)/12)/4+367*($2-2-($2-14)/12*12)/12-3*(($3+4900+($2-14)/12)/100)/4))
echo $JDD
}
# This function computes the gregorian date from the julian date - $1. Returns a
# date strin of the form: MONTH DAY YEAR
# See URL:
for more information
get_greg_from_JD ()
{
typeset -i L
typeset -i N
typeset -i I
typeset -i J
typeset -i DAY
typeset -i MON
typeset -i YR
L=$(($1+68569)) # $1 is the julian date
N=$((4*L/146097))
L=$((L-(146097*N+3)/4))
I=$((4000*(L+1)/1461001))
L=$((L-1461*I/4+31))
J=$((80*L/2447))
DAY=$((L-2447*J/80))
L=$((J/11))
MON=$((J+2-12*L))
YR=$((100*(N-49)+I+L))
echo $MON $DAY $YR
}
TY="20"$(date '+%y') # Year for today
TM=$(date '+%m') # Month for today
TD=$(date '+%d') # Day for today
JD=$(get_JD TD TM TY) # today's Julian date
YJD=$((JD-1)) # yesterday's julian date
date_str=$(get_greg_from_JD YJD)
# parse yesterday's date string
set - $(echo $date_str)
echo $1 # month
echo $2 # day
echo $3 # year