使用时间戳将天数添加到GNU日期命令 [英] Adding days to GNU date command with time stamp

查看:55
本文介绍了使用时间戳将天数添加到GNU日期命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此命令时,我会得到您期望的结果:

When I run this command I get what you'd expect:

date -d "2018-06-07 + 1 days"
Fri Jun  8 00:00:00 CEST 2018

在提供的日期中添加1天(以午夜为起点).

1 day is added to the day provided (using midnight as starting point).

但是,当我尝试一次工作(17:00:00)时,会发生两件事.

However when I try to work in a time (17:00:00), two things happen.

date -d "2018-06-07 17:00:00 + 28 days" 

  1. 长达25天,输出错误:错误的日期/错误的时间(我已将其循环运行).
  2. 在25天以上,它会吐出日期:无效日期'2018-06-07 17:00:00 +25天""

联机帮助页上介绍了-d/-date,它几乎是免费格式.但是我开始认为当您使用时间(小时:分钟:秒)时,加号会被错误地解释(也许是时区偏移?)?

The manpage says about -d /--date that is pretty much free format. But I'm starting to think the plus sign is incorrectly interpreted (maybe as a timezone offset?) when you use the time (hours:minutes:seconds)?

那么我该如何添加带有时间戳记的日期呢?

So how can I add days FROM a timestamped date?

推荐答案

要使具有时间戳记的日期递增,时间戳记必须采用 date 命令默认返回的标准格式.因此,将日期消毒为接受分钟算术并执行处理的格式.

For increment on the days with timestamp to work, the timestamp needs to be in the standard format returned by default by the date command. So sanitize the date to a format in which it accepts minute arithmetic and do the processing.

date -d "2018-06-07 17:00:00"
Thu, Jun 07, 2018  5:00:00 PM

现在将其放在变量中,例如将您的字符串放在下面的示例中

Now put it in a variable, e.g. putting your string in the example below

dateStr=$(date -d "2018-06-07 17:00:00")
date -d "$dateStr + 28 days"

返回

Thu, Jul 05, 2018  5:00:00 PM

该示例使用来自IST的时区.

The example uses timezones from IST.

这篇关于使用时间戳将天数添加到GNU日期命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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