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

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.

Prb in Yest'days Date Format

Reply to Message Icon

Name: Nish
Date: February 2, 2003 at 21:47:50 Pacific
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.



Sponsored Link
Ads by Google

Response Number 1
Name: Frank
Date: February 3, 2003 at 01:00:40 Pacific
Reply:

Hi Nish,

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

This will help.

No RISK no fun
Frank


0

Response Number 2
Name: WilliamRobertson
Date: February 3, 2003 at 16:11:00 Pacific
Reply:

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.


0

Response Number 3
Name: Nish
Date: February 3, 2003 at 18:54:08 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: Prb in Yest'days Date Format

UNIX/changing date format www.computing.net/answers/unix/unixchanging-date-format/8440.html

change date format using shell www.computing.net/answers/unix/change-date-format-using-shell/6741.html

Time diff and date formatting prob www.computing.net/answers/unix/time-diff-and-date-formatting-prob/5065.html