如何将可配置分钟添加到其中包含当前时间的变量[重复]

问题描述 投票:-1回答:1
now=$(date +"%r")

$now目前的时间为12小时。我需要添加可配置的分钟到现在并将其保存在另一个变量中。请帮忙。

linux bash time
1个回答
1
投票

使用date选项很容易做GNU -d

(但是你需要将变量名从now更改为其他名称,因为now-d "datestring"处理中的关键字)

例如:

foo=$(date +%r)        ## save current time in your format in foo
addmin=10              ## variable to add minutes to the time (10 here)
bar=$(date -d "$foo + $addmin minutes" +%r)  ## add minutes to time saving in bar
printf "foo: %s\nbar: %s\n" "$foo" "$bar"    ## output both

示例输出

foo: 09:14:53 PM
bar: 09:24:53 PM
© www.soinside.com 2019 - 2024. All rights reserved.