Operating System - HP-UX
1745854 Members
4245 Online
108723 Solutions
New Discussion

Get next/last month from current date

 
James0077
Occasional Visitor

Get next/last month from current date

Hi all

i want to get next/last month from current date from HP-UX system  (HP-UX B.11.23 )

example :

current day : 160601

i want to get output :

160701 (next month)

160501 (last month)

 

Any suggestion ?

Thx

4 REPLIES 4
Steven Schweda
Honored Contributor

Re: Get next/last month from current date

> i want to get next/last month from current date from HP-UX system

   Are you looking for a shell script, or what?  Assuming a shell
script, how much don't you know how to do?  Some potentially useful
pieces follow.

date='160601'
mba$ echo $date | sed -e 's/\(..\)..../\1/'
16

mba$ echo $date | sed -e 's/..\(..\)../\1/'
06

mba$ echo $date | sed -e 's/....\(..\)/\1/'
01

mba$ m='06'
mba$ echo '-01-02-03-04-05-06-07-08-09-10-11-12-01' | \
  sed -e "s/.*-$m-\(..\).*/\1/"
07

Bill Hassell
Honored Contributor

Re: Get next/last month from current date

There are a lot of requirements that you did not provide.
There is nothing built-in for HP-UX for this requirement.

Here is a scripted solution (two functions) where you can provide any year (00-99) and month (01-12). You simply add these functions to your main program. 

function PrevMON
{

# Provide YYMM01
# Returns YYMM01 for previous month
# Handles year change for MON=1

# Wrong parm count
if [[ $# -ne 1 ]]
then
   echo "999999"
   return
fi

CURRMO=$(echo $1 | cut -c 3-4)
CURRYR=$(echo $1 | cut -c 1-2)

# parms not in range
if [[ $CURRYR < "00" || $CURRYR > "99" ]]
then
   echo "YR??"
   return
fi
if [[ $CURRMO < "01" || $CURRMO > "12" ]]
then
   echo "MO??"
   return
fi

typeset -Z2 YRMINUS=$CURRYR
typeset -Z2 MONMINUS=$(($CURRMO-1))
if [[ $MONMINUS -lt 1 ]]
then
   MONMINUS=12
   YRMINUS=$(($CURRYR-1))
fi

printf "%2s%2s01" $YRMINUS $MONMINUS

}

and 

function NextMON
{

# Provide YYMM01
# Returns YYMM01 for previous month
# Handles year change for MON=1

# Wrong parm count
if [[ $# -ne 1 ]]
then
   echo "999999"
   return
fi

CURRMO=$(echo $1 | cut -c 3-4)
CURRYR=$(echo $1 | cut -c 1-2)

# parms not in range
if [[ $CURRYR < "00" || $CURRYR > "99" ]]
then
   echo "YR??"
   return
fi
if [[ $CURRMO < "01" || $CURRMO > "12" ]]
then
   echo "MO??"
   return
fi

typeset -Z2 YRPLUS=$CURRYR
typeset -Z2 MONPLUS=$(($CURRMO+1))
if [[ $MONPLUS -gt 12 ]]
then
   MONPLUS=1
   YRPLUS=$(($CURRYR+1))
fi

printf "%2s%2s01" $YRPLUS $MONPLUS

}

Examples:

echo $(PrevMON 020201)
020101
echo $(PrevMON 020101)
021201

echo $(NextMON 021101)
021201
echo $(NextMON 021201)
030101

 



Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: Get next/last month from current date

> There are a lot of requirements that you did not provide.

   As usual.

   Also as usual, there are many ways to solve a problem like this.
Many (most?) will involve disassembling the YYMMDD-format date string
into separate YY, MM, and DD pieces; doing the arithmetic/string
processing; and then reassembling the pieces to make a new YYMMDD-format
date string. There is more than one way to do each of these tasks.  (As
shown here.)


> function PrevMON
> [...]
> # Returns YYMM01 for previous month
> # Handles year change for MON=1

> function NextMON
> [...]
> # Returns YYMM01 for previous month
> # Handles year change for MON=1

    Too much copy+paste in the comments.

Bill Hassell
Honored Contributor

Re: Get next/last month from current date

Excellent catch.
Here are fixed lines for the NextMON function:

> # Returns YYMM01 for next month
> # Handles year change for MON=12


Bill Hassell, sysadmin