Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

previous friday date 1

Status
Not open for further replies.

philipose

Programmer
Dec 24, 2003
137
US
Unix Gurus,
How can I get the previous (including today) Friday's date in ddmonyyyy format ?

Thanks in advance
philipose
 
You could also do this - via perl

Code:
PAST=3         #change this to however many days past you want
perl -e "print scalar localtime (time() - 86400 * $PAST); "|awk '{print $3$2$5}'

Based on your description for the date, the above will print in this format: 02Apr2007
 
Thanks sbrews for your help. I forgot to mention that I was using kshell and not perl. Thanks very much

# Get current week day
# %u Displays the weekday as a decimal number from 1-7 (Sunday = 7)
DATEWEEK=`date +"%u"`
# Constants for Friday
FRIDAY=5
# Get Last Friday as ddmonyyyy

DAY=`date +"%d"`
MONTH=`date +"%m"`
YEAR=`date +"%Y"`

COUNTER=0
while [[ $DATEWEEK -ne $FRIDAY ]] ; do
COUNTER=`expr $COUNTER + 1`
DAYS_BACK=`expr $COUNTER \* 24`
DAY=`TZ=CST+$DAYS_BACK date +%d`
echo $DAY
if [[ "$DAY" -eq 0 ]] ; then
MONTH=`expr "$MONTH" - 1`

if [[ "$MONTH" -eq 0 ]] ; then
MONTH=12
YEAR=`expr "$YEAR" - 1`
fi
fi
DATEWEEK=`expr $DATEWEEK - 1`
if [[ $DATEWEEK -eq 0 ]]; then
DATEWEEK=7
fi
done

echo $DAY $MONTH $YEAR
 
That the nice thing about that perl line - it is easily embeded into a ksh script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top