Display month for Previous day

Hello - I have one question regarding the date. I wanted to display the month name for previous day. The output should be as follows...

5-Feb-09 => February
1-Feb-09 => January
28-Feb-09=> February

Here is the code i am using to get the output....

date '+%m %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
}

Based on this code, here is the output.

5-Feb-09 => February(it is correct)
1-Feb-09 => February(it is not correct.. It should display January).
28-Feb-09=> February(it is correct)

On FreeBSD :

# date
Sun Feb  1 11:32:11 EST 2009
# date +%B
February
# date -v -1d
Sat Jan 31 11:32:28 EST 2009
# date -v -1d +%B
January

With GNU date:

date -d -1day +%B

In any POSIX shell:

months="December November October September August July June May April March February January December "
eval "$( date "$@" "+month=%B day=%d" )"
day=$(( $day - 1 ))
if [ $day -eq 0 ]
then
  month=${months#*$month }
  month=${month%% *}
fi
printf "%s\n" "$month"

[INDEnt]Same in "plain english" :slight_smile:

date -d "1 day ago" +%B 

Too prolix! :wink: