在BASH中的特定日期添加X天

编程入门 行业动态 更新时间:2024-10-24 07:34:13
本文介绍了在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天

本文发布于:2023-11-09 23:00:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   BASH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!