Computing.Net > Forums > Unix > Calculate Previous Date in ksh

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Calculate Previous Date in ksh

Reply to Message Icon

Name: Nish
Date: December 1, 2002 at 20:02:02 Pacific
OS: Solaris
CPU/Ram: 256
Comment:

All,
I have a requirement to assign yesterday's date in to a var.
This should be compatible with all the months from Jan to Dec.
Appreciate your help regarding the same.
Cheers,
Nish.



Sponsored Link
Ads by Google

Response Number 1
Name: Jimbo
Date: December 2, 2002 at 05:09:06 Pacific
Reply:

This is not my script; it's one of the better ones i found a while back on comp.unix.shell when i needed this also:

#!/bin/ksh

set -A DAYS Sat Sun Mon Tue Wed Thu Fri Sat
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

YESTERDAY=$((`date +%d` -1))
MONTH=`date +%m`
YEAR=`date +%Y`
NDAY=`date +%u`
WEEKDAY=${DAYS[`date +%u`]}

if [ $YESTERDAY -eq "0" ]
then
MONTH=$((MONTH-1))

if [ $MONTH -eq "0" ]
then
MONTH=12
YEAR=$((YEAR-1))
fi

set `cal $MONTH $YEAR`
shift $(($# - 1))
YESTERDAY=$1
fi

TMONTH=${MONTHS[MONTH]}
# uncomment next line for debugging
# echo $WEEKDAY $YESTERDAY $MONTH $TMONTH $YEAR
echo $YESTERDAY


-jim


0

Response Number 2
Name: Nish
Date: December 3, 2002 at 23:22:26 Pacific
Reply:

Dear Jim,
Great !!!! Txs a Lot !
Nish.



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


unix inodes find out if person messag...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Calculate Previous Date in ksh

calculate previous date www.computing.net/answers/unix/calculate-previous-date/5078.html

Date calculations www.computing.net/answers/unix/date-calculations/3166.html

Calculating 14 days in the future www.computing.net/answers/unix/calculating-14-days-in-the-future/5105.html