Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Date - how to get (year -1)

Regular Advisor

Date - how to get (year -1)

Hi

how to get year-1?

YRLESS=`date +%Y-1`
echo $YRLESS
The above does not work.

Regards
Feng Lin
7 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: Date - how to get (year -1)

You would have to write a program. Or in perl subtract a year from now. But you would have to handle leap years, either Feb 29 doesn't exist or you would have to include an extra day, if you want the month and day to match the current year.
Acclaimed Contributor Acclaimed Contributor

Re: Date - how to get (year -1)

Ah, if you just want to get last year and not a date:
$ YRLESS=$(( $(date +%Y) - 1 ))
Or:
$ (( YRLESS = $(date +%Y) - 1 ))
Honored Contributor Honored Contributor

Re: Date - how to get (year -1)

Hi,
You can do this way, cy=current year, py=previous year

$ cy=date +%Y
$ py=`expr $cy - 1 `
$ echo $py
2009


Suraj
Honored Contributor Honored Contributor

Re: Date - how to get (year -1)

$ date '+%Y-1'|bc
2009

Enjoy, Have FUN! H.Merijn [ could not resist to do it even shorter ]
Enjoy, Have FUN! H.Merijn
Trusted Contributor Trusted Contributor

Re: Date - how to get (year -1)

let YRLESS=`date +%Y`-1
echo $YRLESS

/Tor-Arne
I'm trying to become President of the state I'm in...
Regular Advisor

Re: Date - how to get (year -1)

Thanks to all.

Regards
Feng Lin
Highlighted
Acclaimed Contributor Acclaimed Contributor

Re: Date - how to get (year -1)

>Thanks to all.

If you are happy with the answers you have gotten, please ready the following about how to assign points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33