简体   繁体   English

BASH / Linux-从变量中查找下一个日期

[英]BASH/Linux - Finding the next date from a variable

In my Unix shell programming, I am trying to get the next date (tomorrow's date) over a reference date defined as "a". 在我的Unix Shell编程中,我试图获取定义为“ a”的参考日期后的下一个日期(明天的日期)。 Here is the code: 这是代码:

a=2016-01-02

Which operator would I use in my code so that Unix will automatically define a as tomorrow's date as in below 我将在代码中使用哪个运算符,以便Unix将如下所示自动定义为明天的日期

a=2016-01-03

date has a -d option that is very useful in this situation. date具有-d选项,在这种情况下非常有用。

To get the next day, add a space after the date then add 1 day 要获得第二天,请在日期之后添加一个空格,然后添加1 day

date +%Y-%m-%d -d "$a 1 day"

It's important to add the format specifier because without it, you would get the following output 添加格式说明符很重要,因为没有它,您将得到以下输出

=>"Sun Jan 3 00:00:00  UTC 2016"

To update the a variable, you could do something like this 要更新的a变量,你可以做这样的事情

a=$(date +%Y-%m-%d -d "$a 1 day")

Remember to wrap the command inside of parentheses with a $ sign in front of it. 请记住,将命令包装在括号内,并在其前面加一个$号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM