now=$(date +"%r")
$now
目前的时间为12小时。我需要添加可配置的分钟到现在并将其保存在另一个变量中。请帮忙。
使用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