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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get date in this format

Status
Not open for further replies.

dvknn

IS-IT--Management
Mar 11, 2003
94
0
0
US
Hi,
From `date +%d"-"%b"-"%Y` , I get for ex: 06-AUG-2004.

How can I get 06-JUL-2004 (I would like get always
currentday-previuosmonth-currentyear)

Thanks a bunch..
 
This should work for you....



#!/usr/bin/ksh -p

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

if [ ${MON} -eq 1 ]; then
MON=12
else
let MON=MON-1
fi

case ${MON} in
1) AM="January";;
2) AM="Feburary";;
3) AM="March";;
4) AM="April";;
5) AM="May";;
6) AM="June";;
7) AM="July";;
8) AM="August";;
9) AM="September";;
10) AM="October";;
11) AM="November";;
12) AM="December";;
esac

print "${DAY}-${AM}-${YEAR}"
# EOS
 
currentday-previousmonth-currentyear
And what happens if today is 31-July ?
 
Thank You PMC.

PHV,

It doesn't matter.. As long as the month is the previous month always..Is there some other way other than the one indicated above by PMC?

Thanks
 
It doesn't matter
So for you, 31-June or 30-February are OK ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Huh!!!!

I thought
DAY=`date "+%d"` would take care of the day..Would it not??

Am I missing something?

Thanks
 
dvknn,

PHV is right, the day if July 31 would be reported as June 31, 2004. Since this is not changing the TZ value in the OS, then this would be incorrect.

This should resolve the issue PHV brought up.

#!/usr/bin/ksh -p

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

set -A M30 "April" "June" "September" "November"

if [ ${MON} -eq 1 ]; then
MON=12
else
let MON=MON-1
fi

case ${MON} in
1) AM="January";;
2) AM="Feburary";;
3) AM="March";;
4) AM="April";;
5) AM="May";;
6) AM="June";;
7) AM="July";;
8) AM="August";;
9) AM="September";;
10) AM="October";;
11) AM="November";;
12) AM="December";;
esac

if [ ${DAY} -eq 31 ]; then
for ${MONTH} in "${M30[@]}"; do
if [ "${AM}" == "${MONTH}" ]; then
DAY=30
fi
done
fi
if [[ ${DAY} -eq 30 && "${AM}" == "Feburary" ]]; then
YRMOD=`expr ${YEAR} % 4`
[ ${YRMOD} -eq 0 ] && DAY=29 || DAY=28
fi

print "${DAY}-${AM}-${YEAR}"
# EOS
 
Whoops...moving to fast, replace this:

if [ ${DAY} -eq 31 ]; then
for ${MONTH} in "${M30[@]}"; do
if [ "${AM}" == "${MONTH}" ]; then
DAY=30
fi
done
fi

with this instead:

if [ ${DAY} -eq 31 ]; then
for MONTH in "${M30[@]}"; do
if [ "${AM}" == "${MONTH}" ]; then
DAY=30
fi
done
fi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top