问题描述
假设今天的日期格式为YYYYMMDDHHMMSS,例如20160720152654.我想添加几小时,几分钟或几秒钟的日期.
Suppose todays' date is formatted to YYYYMMDDHHMMSS such as 20160720152654. I would want to add hours or minutes or seconds to date.
添加1小时应将日期更改为20160720162654加1分钟应将日期更改为20160720152754加1秒应将日期更改为20160720152655
Add 1 hour should change date to 20160720162654Add 1 minute should change date to 20160720152754Add 1 second should change date to 20160720152655
这似乎给了我错误的结果
This seems to give me incorrect results
d = 日期+%Y%m%d%H%M%S
pd = $((($ d +($ d%(15 * 60)))))回声$ d回声$ pd
d=date +%Y%m%d%H%M%S
pd=$(($d + ($d % (15 * 60))))echo $decho $pd
输出2016072015514120160720155482
Output2016072015514120160720155482
推荐答案
您可以操纵输入以将其传递给 date -d
:
You can manipulate input to pass it to date -d
:
s='20160720162654'
# add 1 minute
date -d "${s:0:8} ${s:8:2}:${s:10:2}:${s:12:2} +1 min" '+%Y%m%d%H%M%S'
20160720112754
# add 1 sec
date -d "${s:0:8} ${s:8:2}:${s:10:2}:${s:12:2} +1 sec" '+%Y%m%d%H%M%S'
20160720112655
# add 1 hour
date -d "${s:0:8} ${s:8:2}:${s:10:2}:${s:12:2} +1 hour" '+%Y%m%d%H%M%S'
20160720122654
这篇关于使用bash在YYYYMMDDHHMMSS中增加日期的小时/分钟/秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!