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

Find previous month using DATE (date +%b)

Status
Not open for further replies.

lleemon

Programmer
Mar 26, 2002
21
GB
Is it possible to find the previous month using DATE?
I can find the current by doing:
x=`date +%b`

Anyone know how to find the previous month?
Thanks.
 
Take a look at this faq.

faq80-953

You may be able to modify to code
do what you want.

Robert Robert G. Jordan
Unix Sys Admin
Sleepy Hollow, Illinois U.S.A.
sh.gif


FREE Unix Scripts
 
FAQ80-953 is for previous date. Your basically changing the time zone by 24 hours so you can get previous date or next days date. This will not work if it's June 15 and I need to know that last month was May.

Thanks.
 
faq80-2236 can be modified for your purpose -

Code:
# File-name: last_month.sh
#--------------------------------------
# Returns the last month
#--------------------------------------
# Input:                Default:
#   $1 - mm             Current month
#   $2 - yyyy           Current year
#--------------------------------------

#This is how I define a function in UNIX
#shell scripts
get_one_day_before_specified_date()
{
    # get the command line input (date,month & year)
    month=$1
    year=$2
 
        # if it is the first month of the year
        if [ $month -eq 01 ]
        then
            # make the month as 12
            month=12
 
            # deduct the year by one
            year=`expr $year - 1`
        else
            # deduct the month by one
            month=`expr $month - 1`
        fi

typeset -Z2 month=${month}
        
    echo $month $year
}

#!/bin/ksh
if [ $# -ne 2 ]
then
    m=`date +%m`
    y=`date +%Y`
else
    m=$1
    y=$2
fi    

get_one_day_before_specified_date $m $y
-Vikram Kalsi
----------------------------------------------
We learn most when we have to invent - Piaget
 
vikramkalsi - Thanks for the code but I believe the output of this will be: 06 2002.

If the current date is: Jul 2002.
I want to have Jun 2002 be returned.

Thanks.
 
Pipe date +%b to this awk script.

BEGIN {
m["Jan"]="Dec";m["Feb"]="Jan";m["Mar"]="Feb"
m["Apr"]="Mar";m["May"]="Apr";m["Jun"]="May"
m["Jul"]="Jun";m["Aug"]="Jul";m["Sep"]="Aug"
m["Oct"]="Sep";m["Nov"]="Oct";m["Dec"]="Nov"
}
{
print m[$0]
} CaKiwi
 
Hi lleemon,
That should not be difficult -

In the last_month.sh script replace

--------Start Code--------------
Code:
typeset -Z2 month=${month}
echo $month $year
--------End Code--------------

With

--------Start Code--------------
Code:
case $month in
1) mon="Jan";;
2) mon="Feb";;
3) mon="Mar";;
4) mon="Apr";;
5) mon="May";;
6) mon="Jun";;
7) mon="Jul";;
8) mon="Aug";;
9) mon="Sep";;
10) mon="Oct";;
11) mon="Nov";;
12) mon="Dec";;
esac

echo $mon $year
--------End Code-------------- -Vikram Kalsi
----------------------------------------------
We learn most when we have to invent - Piaget
 
CaKiwi -
Can I run this within a perl script?

Thanks to all that have posted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top