Reputation: 41
Year=`date '+%Y'`
RTRN1=$?
This returns the current date in the logs, however i want to return the year before, so instead of this returning 2017 i want 2016.
Any help appreciated! Thanks
Upvotes: 4
Views: 5100
Reputation: 84607
You can always capture the year with date +'%Y'
. You can subtract 1
with the POSIX arithmetic operator, e.g.
$ echo $(($(date +%Y) - 1))
2016
You can also use the POSIX compliant expr
math operators, e.g.
$ expr $(date +%Y) - 1
2016
(note: with expr
you must leave a space
between the math operator and the values)
The GNU date operator -d
with '1 year ago'
will work as specified in the comments and other answer, along with let dt=$(date +%Y)-1; echo $dt
as specified in the other answer (no spaces allowed with let
).
Of all the choices, if I didn't have GNU date
, I'd pick the POSIX arithmetic operator $((...))
with a date
command substitution minus 1
.
Upvotes: 2
Reputation: 15186
As we have bash
, it's possible to use let
let YEAR=`date +%Y`-1
echo $YEAR
Upvotes: 1
Reputation: 92894
For GNU date
utility: use -d
(--date
) option to adjust the date:
Year=$(date +%Y -d'1 year ago')
echo $Year
2016
Upvotes: 7