在BASH中的特定日期添加X天 [英] Add X days to a particular date in BASH

查看:95
本文介绍了在BASH中的特定日期添加X天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对BASH来说是全新的.提前致歉.

Totally new to BASH. Apologies in advance.

问题

我想在特定日期前加上X天.

I'd like to add X days to a specific date.

代码

我发现BASH中的date会检索当前日期.

I figured out that date in BASH retrieves the current date.

我还发现我可以通过以下方式将X天添加到当前日期,

I also figured out that I can add X days to the current date in the following way,

expiration_date=$ date -v +1d

给出

Tue Sep 26 20:28:13 CEST 2017 

实际上是撰写日期加上X=1天.

which is indeed the date of writing plus X=1 days.

问题

我想插入一个要添加X天的特定日期,而不是上面的命令行中的date. 2017年9月20日.

In stead of date in the command line above, I'd like to insert a particular date to which X days will be added, e.g. 20/09/2017.

不在乎特定日期的格式.

Don't care about the format of the particular date.

换句话说:我如何进行以下工作

In other words: How do I make the following work,

expiration_date=$ '20/09/2017' -v +1d


尝试了此答案,但是没有.做我想做的.


Tried this answer, but doesn't do what I want.

不知道OSX会有什么不同.

Did not know things are different for OSX.

推荐答案

您可以这样做:

dt='2017-09-20'

date -d "$dt +1 day"
Thu Sep 21 00:00:00 EDT 2017

date -d "$dt +2 day"
Fri Sep 22 00:00:00 EDT 2017


似乎OP正在使用OSX.您可以通过以下方式使用日期添加:


It seems OP is using OSX. You can use date addition this way:

s='20/09/2017'
date -j -v +1d -f "%d/%m/%Y" "$s"

Thu Sep 21 14:49:51 EDT 2017

这篇关于在BASH中的特定日期添加X天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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