Korn Shell Script - Getting yesterdays date

I need to get yesterdays date in the format yyyymmdd
I can get today's date simply enough - 20031112

Is there any way to substract 1 from this easily enough in korn shell script?

It has to be korn shell and not perl

Hi,

You can use the script below, it will do more than you asked, but you can easly modify it... it was written by Tapani Tarvainen.

#! /usr/bin/ksh
# Get yesterday's date in YYYY-MM-DD format.
# With argument N in range 1..28 gets date N days before.
# Tapani Tarvainen January 2002
# This code is in the public domain.

OFFSET=${1:-1}

case $OFFSET in
*[!0-9]* | ???* | 3? | 29) print -u2 "Invalid input" ; exit 1;;
esac

eval `date "+day=%d; month=%m; year=%Y`
typeset -Z2 day month
typeset -Z4 year

# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=$((day - OFFSET))
if (( day <= 0 )) ;then
month=$((month - 1))
if (( month == 0 )) ;then
year=$((year - 1))
month=12
fi
set -A days `cal $month $year`
xday=${days[$(( ${#days[*]}-1 ))]}
day=$((xday + day))
fi

print $year-$month-$day
print $month/$day/${year#??}

Thanks - I have found a way myself now...

datestamp=`date '+%Y%m%d'`
yest=$((datestamp -1))

Cheers!!

1 Like

That won't work on the first of the month.

Here is a script that can do date calculations without the use of external programs.

Hi- Thanks - to be honest I hadnt thought of the first of the month

I am using your script as follows:
./datecalc -a 2003 11 12 - 1

and it returns 2003 11 11

Is there a way that it can return the value without the spaces?

./datecalc -a 2003 11 12 - 1 | read y m d ; echo ${y}${m}${d}

# ./datecalc -a 2003 11 12 - 1 | read y m d ; echo ${y}${m}${d}
y: Undefined variable

any ideas?

Another method could be to run a script at midnight via cron

0 0 * * */path/to/script.sh

mv /path/to/today.txt /path/to/yesterday.txt
date '+%Y%m%d' > /path/to/tovay.txt

So whenever a script wants to have yesterday's date, you get it from the file

yest=$(</path/to/yesterday.txt)

In your first post you specified ksh. That is why I posted ksh code. It will not work with other shells. Switch to ksh and try it again.

Please try this out.
./datecalc -a 2003 11 12 - 1
let the o/p is 2003 11 11
so
dt=2003 11 11
echo $dt | tr -d [[:blank:]]
:wink:

If you are happy for the new (yesterday) date in a variable then try

$> YESTERDAY=`TZ=aaa24 date +%Y%m%d`
$> echo $YESTERDAY
20070729
$> 

1) Are we sure this will work on the first of the month as well?
2) I am also needing yesterday's date, but in a different format: 29-Jul-2007 (so I have to figure out how to put hyphens between %d, %b, and %Y...)
3) Finally, what does "aaa24" mean?

To get the todays date

echo $(date "+%G%m%d") 

Yesterday date

echo $(date -d "1 day ago" "+%G%m%d")

Oops, I didnt check that it was a old request :wink:

Hi,

1) I've never had a problem with the first of the month. Used this quiete a lot for reporting previous days work.

2) man date. This will give the options for date string formatting. But to add the - you'll need to quote them.

3) aaa is just a string (could be anything, within reason) the off set 24 is important part.

Here I use a different string name and offset of 48, gives two days ago date.

YESTERDAY=`TZ=y48 date +%Y"-"%m"-"%d`

2007-07-31

Hi Unix gurus,

Anyone has any scripts out there that determines if current date is the first working day of the month? For example, it should be 2007-OCT-01, 2007-NOV-01, 2007-DEC-03, 2008-JAN-01, 2008-FEB-01, 2008-MAR-03, and so on..

I need this logic incorporated in my script that generates reports on every 1st day of the month. We would prefer not to use cron jobs here.

Any inputs would be much appreciated.

Thanks in advance.

first_day.sh NOV 2007
Your 1st working day of 2007/NOV is : 01-NOV-2007
first_day.sh FEB 2008
your 1st working day of 2008/FEB is : 01-FEB-2008
first_day.sh MAR 2008
your 1st working day of 2008/MAR is : 03-MAR-2008
first_day.sh OCT 2009
your 1st working day of 2009/OCT is : 01-OCT-2009

Try this one as your test, and modify as per your requirements,

#!/bin/sh
function WeekDay
{
###################################################
#Function Name WeekDay()
#Return Value will be one of the following values in WDay
#0,1,2,3,4,5,6
#0-SUNDAY
#1-MONDAY
#2-TUESDAY
#3-WEDNESDAY
#4-THURSDAY
#5-FRIDAY
#6-SATURDAY
###################################################
year=$2
month=$1
day=01
a=`expr 14 - $month`
let a=a/12
let y=year-a
ma=`expr 12 \* $a`
m=`expr $month + $ma - 2`
let y4=y/4
let y100=y/100
let y400=y/400
let m31=m*31
let m12=m31/12
add=`expr $day + $y4 + $y - $y100 + $y400 + $m12`
let wday=add%7
WDay=$wday
}

function firstDay
{
MM=$1
YYYY=$2

    case "$MM" in
    JAN\)    mm=1    ;;
    FEB\)    mm=2    ;;
    MAR\)    mm=3    ;;
    APR\)    mm=4    ;;
    MAY\)    mm=5    ;;
    JUN\)    mm=6    ;;
    JUL\)    mm=7    ;;
    AUG\)    mm=8    ;;
    SEP\)    mm=9    ;;
    OCT\)    mm=10   ;;
    NOV\)    mm=11   ;;
    DEC\)    mm=12   ;;
    *\)      echo "Wrong month specified."
            exit 1 ;;
    esac

    WeekDay $mm $YYYY
    if [ $WDay -eq 6 ]      \# if 01 of your month and year falls on SATURDAY then
    then                    \# 1st working day will be 03
            echo " your 1st working day of $YYYY/$MM is : 03-$MM-$YYYY"
    elif [ $WDay -eq 0 ]   \# if 01 of your month and year falls on SUNDAY then
    then                    \# 1st working day will be 02
            echo " your 1st working day of $YYYY/$MM is : 02-$MM-$YYYY"
    else
            \# Then as usual default 1st is the 1st working day
            echo " your 1st working day of $YYYY/$MM is : 01-$MM-$YYYY"
    fi

}

firstDay $1 $2

Anyone have a script to add number of hours to a date field. Assuming that I have the following date and time:

                  25/01/2008 02:19:35

Thanks in advance.

Cheers.

simple command to get yesterdays date:
TZ=aaa24 date +%Y%m%d
in yyyymmdd format

Hi try the below one:

date '+%d/%m/%Y %T'

to get in 25/01/2008 02:19:35 format