Computing.Net > Forums > Unix > Prb in Yest'days Date Format

Prb in Yest'days Date Format

Reply to Message Icon

Original Message
Name: Nish
Date: February 2, 2003 at 21:47:50 Pacific
Subject: Prb in Yest'days Date Format
OS: Solaris
CPU/Ram: 256
Comment:

Dear All,
I'm using following script to calculate Yesterday's Date as a portion of a huge script.

#!/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]}
$YEAR
echo $YESTERDAY$MONTH$YEAR

Date format that I need is yymmdd (030203). When d or m lessthan 10 required date format doesn't maintain.
e.g. if the date is 030203 it displays Yesterday's date as 03022 (which should be 030202).
Appreciate your help to resolve this Prb.


Report Offensive Message For Removal


Response Number 1
Name: Frank
Date: February 3, 2003 at 01:00:40 Pacific
Subject: Prb in Yest'days Date Format
Reply: (edit)

Hi Nish,

use
typeset -Z2 YESTERDAY=$((`date +%d` -1))

This will help.

No RISK no fun
Frank


Report Offensive Follow Up For Removal

Response Number 2
Name: WilliamRobertson
Date: February 3, 2003 at 16:11:00 Pacific
Subject: Prb in Yest'days Date Format
Reply: (edit)

I think to do this properly you have to convert to a julian date format, do the arithmetic, and convert back. I had to write something similar recently and I started out trying to do it the easy way and it just got difficult. It's a bit long to post here but it's at http://www.williamrobertson.pwp.blueyonder.co.uk/code/shift_date.txt (or www.williamrobertson.net, Korn shell, shift_date).

btw, why declare #!/bin/ksh then use sh syntax with
if [ $MONTH -eq "0" ]

when you can do it in ksh with
if [[ $MONTH = 0 ]]
or even (if MONTH is an integer though I don't see it declared as one)
if (( MONTH == 0 ))

Also the usual convention is uppercase for exported variables, lowercase for others.


Report Offensive Follow Up For Removal

Response Number 3
Name: Nish
Date: February 3, 2003 at 18:54:08 Pacific
Subject: Prb in Yest'days Date Format
Reply: (edit)

Dear Frank/William,
Thanks a Lot for your help !
Cheers,
Nish.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Prb in Yest'days Date Format

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge