get yesterday in yyyymmdd format

how can i get yesterday in yyyymmdd format? :confused:

with gnu date you can do:

date --date="-1 days" +%Y%m%d

if you have issue with the above method say you dont have gnu date... try connecting to oracle and get date from there...as there is no direct mechanism to calculate date.

something like this ...

yesterday=`sqlplus user/password << EOF
Select to_char(sysdate-1,'YYYYMMDD') from dual
Exit
EOF`

I wrote this script to convert julian date into dd-mm-yyyy format. You could use this to get yesterday's date. You just have to get today's julian date (get this using `date +%j`) and subtract 1 from it. This will get you yesterday's julian date. Give this as the second argument to the script. The first argument is the present year in the YYYY format.

Note: I have changed the output so that it prints YYYYmmdd.

Edit:

Also this script will blindly convert whatever the julian date that is given to it. No error checking is done - so if you give the julian date as 0, the output you get for this year is 20050100

#!/bin/sh

check_done() {
        if [ $month -eq 1 -o $month -eq 3 -o $month -eq 5 -o $month -eq 7 -o $mo
nth -eq 8 -o $month -eq 10 -o $month -eq 12 ]
        then
                daysofmth=31
        elif [ $month -eq 2 ]
        then
                if [ `expr $year % 100` -eq 0 -a `expr $year % 400` -eq 0 ]
                then
                        daysofmth=29
                elif [ `expr $year % 100` -ne 0 -a `expr $year % 4` -eq 0 ]
                then
                        daysofmth=29
                else
                        daysofmth=28
                fi
        elif [ $month -eq 4 -o $month -eq 6 -o $month -eq 9 -o $month -eq 11 ]
        then
                daysofmth=30
        fi
        julday=`expr $julday - $daysofmth`
        if [ $julday -lt 0 ]
        then
                done=1
                julday=`expr $daysofmth + $julday`
        elif [ $julday -eq 0 ]
        then
                done=1
                julday=$daysofmth
        fi
}
#########  main script starts here
if [ $# -ne 2 ]
then
        echo "Usage: fromjul <yyyy> <julian day>"
        exit 1
fi

year=$1
julday=$2

month=0
done=0

while [ $done -ne 1 ]
do
        month=`expr $month + 1`
        check_done
done

printf "%.4d%.2d%.2d\n" $year $month $julday

This does works exept for the 1st day of the month.

date | awk '{printf"%4d%2d%2d\n",$6,$2,($3-1)}' | sed 's/ /0/g'
date | awk '{printf"%4d%2d%2d\n",$6,$2,($3-1)}' | sed 's/ /0/g'

i dont think the above one would work... the second parameter ($2) is displayed as such for month .. which is string format (ex: jul) but required output is in numeric format (07 - for jul)

here is the modification of the aboe one:

date '+%y:%m:%d' | awk -F":" '{printf"20%2d%2d%2d\n",$1,$2,($3-1)}' | sed 's/ /0/g'

My env is Japanese, so this happened.
Thank you for modification!

Try...

Yesterday=$(perl -e '@y=localtime(time()-86400);printf "%04d%02d%02d",$y[5]+1900,$y[4]+1,$y[3];')

Modified the codes from: How to find Previous date and Coming date
to give at least past few days (tested till 10). Just to share.

date '+%m %d %Y' | 
{ 
read MONTH DAY YEAR
MINUS=10
TAIL_NO=`expr "$MINUS" - "4"` 
DAY=`expr "$DAY" - "$MINUS"`

#echo "DAY: "$DAY
#echo "TAIL: "$TAIL_NO

if [ "$DAY" -lt 1 ]
then 
       MONTH=`expr "$MONTH" - 1` 
                case "$MONTH" in 
                        0) 
                           MONTH=12 
                           YEAR=`expr "$YEAR" - 1` 
                        ;; 
                esac 
        DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -$TAIL_NO | head -1` 
fi 

if [ $MONTH -lt 10 ]
then
	MONTH="0"${MONTH}
fi
if [ $DAY -lt 10 ]
then
	DAY="0"${DAY}
fi
date
echo "${MINUS} day(s) ago was: ${YEAR}${MONTH}${DAY}" 
}

I found this on the Internet once:

YESTERDAY=`TZ=GMT+24 date "+%Y%m%d"`

Works great for ksh in HP-UX 11i.

For FreeBSD

date -v -1d "+%Y%m%d"

This doesn't work on 02/01/2008 going back 1 day..

tail: unrecognized option `--3'
Try `tail --help' for more information.
tmp2.sh: line 27: [: -lt: unary operator expected
Fri Feb 1 15:03:29 CST 2008
1 day(s) ago was: 200801
MSIConv:~ # date
Fri Feb 1 15:03:40 CST 2008

date '+20%y/%m/%d' | awk -F"/" '{print $1$2($3-1)}'

output will be yesterday in YYYYMMDD format

arun
try your command on January 1 :

echo "2008/01/01" | awk -F"/" '{print $1$2($3-1)}'