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!

date problem

Status
Not open for further replies.

hcclnoodles

IS-IT--Management
Jun 3, 2004
123
GB
Hi there I currently use a line in a script

ndate=$(date +"%d/%m/%Y")

This obviously returns the date in a format i have chosen. However, I need to come up with a solution for weekends and as such need a way of returning todays date minus two days in the same format (24/11/2004)

so for example if today was monday and the command ndate=$(date +"%d/%m/%Y") returned 17/01/2005, I would want to redo that command so that it would return 15/01/2005 (saturdays date)

any ideas
 
This is a script I've used to accomplish a similar task. It's pretty straight-forward, but also quite long. I'm sure there are better ways, but this will get you going until somebody else responds with a 2 or 3 line awk syntax:

(By the way, I'm not the author of this code, so take no credit:)

#!/bin/ksh
# /usr/local/bin/when
# usage "when 7" to go back 7 days, "when 14" to go back 14, etc.
# useless to go back more than a month
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
BACK=$1
if [ "$BACK" -gt "28" ]
then
echo "Sorry. Too far"
exit
fi
THEN=$((`date +%d` - $BACK))
MONTH=`date +%m`
YEAR=`date +%Y`
if [ $THEN -le "0" ]
then
MONTH=$((MONTH-1))
if [ $MONTH -eq "0" ]
then
MONTH=12
YEAR=$((YEAR-1))
fi
set `cal $MONTH $YEAR`
SHIFT=$(( $THEN * -1 + 1 ))
shift $(($# - $SHIFT))
THEN=$1
fi
TMONTH=${MONTHS[MONTH]}
echo $TMONTH $THEN
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top