在shell脚本中将月份添加到可变日期 [英] Add month to a variable date in shell script

查看:385
本文介绍了在shell脚本中将月份添加到可变日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期要传递到shell脚本中,并且必须在其中加上一个月. 例如:

I have a date being passed into a shell script and have to add a month to that. for eg:

passed_date=2017-06-01

我需要添加1个月:

converted_date=2017-07-01

如何在Shell脚本中实现此目的. 我尝试将日期从纪元转换为秒,然后添加1个月,例如:

How can i achieve this in a shell script. I tried converting date to seconds since epoch and then adding 1 month like:

date +%s -d 20170601 -d "+1 month"

,然后通过

date -d@$(date +%s -d 20170601 -d "+1 month") +%Y-%m-%d

,但它基本上将当前系统日期增加了1个月

but its basically adding 1 month to current system date

推荐答案

您似乎正在寻找:

date -d "20170601+1 month" +%Y-%m-%d

在同一命令中使用多个-d标志时,date似乎仅使用最后一个.

When using multiple -d flags in the same command, date seems to only use the last one.

当然,可以随时用包含任何日期的$VAR替换20170601.

And of course, feel free to replace 20170601 by $VAR containing any date.

这篇关于在shell脚本中将月份添加到可变日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆