计算时间(增加分钟)bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21208473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:37:49  来源:igfitidea点击:

Calculating time (adding minutes) bash

bashtimedate-arithmetic

提问by Kysu

I got stuck in part of the script. I have time: for example "16:00" and duration in minutes like: 410.

我陷入了脚本的一部分。我有时间:例如“16:00”和持续时间(以分钟为单位):410。

Is there any easy way to add those two values? I've tried a lot of combinations with date -d, but can't solve it.

有什么简单的方法可以添加这两个值吗?我尝试了很多与 的组合date -d,但无法解决。

回答by joeytwiddle

Try this (Kysu's version):

试试这个(Kysu 的版本):

date -d "16:00 410 minutes" +'%H:%M'

or this:

或这个:

date -d "16:00 today + 410 minutes" +'%H:%M'

But do notuse this:

不要使用这个:

date -d "16:00 + 410 minutes" +'%H:%M'   # BAD!

Strange things happen if you omit the word todaybut keep the +. (I think the + 410is being parsed as a timezone modifier, and then the minutesis interpreted as "add one minute".)

如果你省略这个词today但保留+. (我认为+ 410被解析为时区修饰符,然后minutes被解释为“增加一分钟”。)