Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I get yesterday's date ?

81 views
Skip to first unread message

richyts

unread,
Jan 23, 2002, 3:15:01 AM1/23/02
to
what could be the simplest way of obtaining yesterday's date in a
shell script ?.. i can use bsh, csh, ksh and awk on aix4.3.

Michael

unread,
Jan 23, 2002, 8:32:20 AM1/23/02
to
ric...@yahoo.com (richyts) wrote in message news:<d1c185ef.0201...@posting.google.com>...

> what could be the simplest way of obtaining yesterday's date in a
> shell script ?.. i can use bsh, csh, ksh and awk on aix4.3.

the easiest way i can think of is creating a crontab entry
59 23 * * * date > /tmp/date_yesterday
and use content of that file as input. If you need other formats -
create other files with formats you need.

hth
Michael

Ken Baldwin

unread,
Jan 23, 2002, 9:27:40 AM1/23/02
to
ric...@yahoo.com (richyts) wrote in message news:<d1c185ef.0201...@posting.google.com>...
> what could be the simplest way of obtaining yesterday's date in a
> shell script ?.. i can use bsh, csh, ksh and awk on aix4.3.

I know it's cheating, and others may have a better solution, but I've
handled this issue by saving the current date to a file. I have a
script that runs daily and it renames the existing file to
'yesterday', then saves the current date to 'today'. Then your script
can just 'cat yesterday' to get the prior days date.

Mark van Huijstee

unread,
Jan 23, 2002, 10:14:06 AM1/23/02
to

On 23 Jan 2002 00:15:01 -0800, ric...@yahoo.com (richyts) wrote:

>what could be the simplest way of obtaining yesterday's date in a
>shell script ?.. i can use bsh, csh, ksh and awk on aix4.3.

Not sure who the person was who created/posted it, but some months ago
the following script was posted. He/she probably doesn't mind me
posting it again :)

Mark

#!/bin/ksh

DATE="/bin/date" # date
typeset -L2 YMONTH # Yesterday month
typeset -R2 YDAY # Yesterday day
typeset -R4 YYEAR # Yesterday year
typeset -Z8 YDATE

YMONTH=$($DATE +%m)
YDAY=$($DATE +%d)
YYEAR=$($DATE +%Y)

if [[ $YDAY = 01 ]] && [[ $YMONTH = 01 ]]
then
((YDAY=31))
((YMONTH=12))
((YYEAR=YYEAR - 1))
elif [[ $YDAY = 01 ]] && [[ $YMONTH = @(02|04|06|08|11) ]]
then
((YDAY=31))
((YMONTH=YMONTH - 1))
elif [[ $YDAY = 01 ]] && [[ $YMONTH = @(05|07|10|12) ]]
then
((YDAY=30))
((YMONTH=YMONTH - 1))
elif [[ $YDAY = 01 ]] && [[ $YMONTH = 03 ]]
then
if ($YYEAR % 400 == 0)
then
((YDAY=29))
((YMONTH=YMONTH - 1))
else
((YDAY=28))
((YMONTH=YMONTH - 1))
fi
else
((YDAY=YDAY-1))
fi

YDATE=$YMONTH$YDAY$YYEAR
print "$YDATE"

Ron Visser

unread,
Jan 23, 2002, 10:44:50 AM1/23/02
to
You could change the TZ variable and add 24 hours to it.
Our current setting is

TZ=NFT-1DFT,M3.5.0,M10.4.0

If I temporarily change this to

TZ=NFT+23DFT,M3.5.0,M10.4.0
^^^
I get yesterdays date from the date command.
In this way you let the date command handle leap years and such.
You can use the "date +FieldDescriptor" syntax to get specific parts of the date.

Regards,
Ron.

ric...@yahoo.com (richyts) wrote in message news:<d1c185ef.0201...@posting.google.com>...

Dale Talcott

unread,
Jan 23, 2002, 12:30:55 PM1/23/02
to
ric...@yahoo.com (richyts) writes:

>what could be the simplest way of obtaining yesterday's date in a
>shell script ?.. i can use bsh, csh, ksh and awk on aix4.3.

Play with the TZ variable. The following ksh script can be used as a
starting point:

--- cut here for tz.sh ---
#!/bin/ksh
# Usage tz.sh N [fmt_string]
# Where N is how many days to go backward
# fmt_string is optional format arg to date command

days=$1;shift

date "+%H %Z" | read t1 tn
t0=$(TZ=0 date +%H)
(( delta = t0 - t1 ))
(( adjust = delta + 24 * days ))

TZ="$tn$adjust" date "$@"

--- end of tz.sh ---

tz.sh 1 # gives you the date and time 24 hours ago
tz.sh 1 '+%d' # gives you just yesterday's day of the month
tz.sh 1 '+%Y%m%d' # gives you yesterday as yyyymmdd
tz.sh -1 # gives you tomorrow

--
Dale Talcott, Purdue University Computing Center
a...@quest.cc.purdue.edu http://quest.cc.purdue.edu/~aeh/

richyts

unread,
Jan 25, 2002, 12:47:44 AM1/25/02
to
a...@quest.cc.purdue.edu (Dale Talcott) wrote in message news:<aeh.101...@quest.cc.purdue.edu>...

> ric...@yahoo.com (richyts) writes:
>
> >what could be the simplest way of obtaining yesterday's date in a
> >shell script ?.. i can use bsh, csh, ksh and awk on aix4.3.
>
> Play with the TZ variable. The following ksh script can be used as a
> starting point:
>
> --- cut here for tz.sh ---
> #!/bin/ksh
> # Usage tz.sh N [fmt_string]
> # Where N is how many days to go backward
> # fmt_string is optional format arg to date command
>
> days=$1;shift
>
> date "+%H %Z" | read t1 tn
> t0=$(TZ=0 date +%H)
> (( delta = t0 - t1 ))
> (( adjust = delta + 24 * days ))
>
> TZ="$tn$adjust" date "$@"
>
> --- end of tz.sh ---
>
> tz.sh 1 # gives you the date and time 24 hours ago
> tz.sh 1 '+%d' # gives you just yesterday's day of the month
> tz.sh 1 '+%Y%m%d' # gives you yesterday as yyyymmdd
> tz.sh -1 # gives you tomorrow


this one works cleanly... thanks a lot

0 new messages