How to subtract a year from a date stored in a var

2020-03-27 06:48发布

as_of_dt='2016-01-01'
as_of_dt_prev_year=$($as_of_dt -d '-1 year' +'%Y-%m-%d')
echo $as_of_dt_prev_year

This does not work. error: -d: command not found

However this works if we use '$date' instead of $as_of_dt.

标签: linux date sh
1条回答
相关推荐>>
2楼-- · 2020-03-27 07:38

Played around with it. This seems to work:

as_of_dt='2016-01-01'
as_of_dt_prev_year=$(date --date="${as_of_dt} -1 year" +'%Y-%m-%d')
echo $as_of_dt_prev_year

Note the double quotes that are needed for variable substitution to work.

查看更多
登录 后发表回答