我无法弄清楚我在哪里犯错。下面以指定格式添加分钟的语法正确吗?
startDate=2018-05-01
sDate=$(date -d "$startDate" '+%Y-%m-%dT%H:%M:%S.%3NZ' )
offset=5
midDate=${sDate}
echo 'mid Date before operation' ${midDate}
midDate=$(date -d "${midDate:0:4}-${midDate:5:2}-${midDate:8:2}T${midDate:11:2}:${midDate:14:2}:${midDate:17:2}.${midDate:20:3} + ${offset} minutes " '+%Y-%m-%dT%H:%M:%S.%3NZ' )
echo 'mid Date after operation' ${midDate}
输出
$bash -f main.sh
mid Date before operation 2018-05-01T00:00:00.000Z
mid Date after operation 2018-04-30T19:01:00.000Z
答案 0 :(得分:0)
尝试使用unix时间工作,例如
#!/bin/sh
start_date=2018-05-01
offset_mins=5
start_unix=$(date -d "${start_date}" +%s)
end_unix=$((start_unix + 60*offset_mins))
end_date=$(date -d "@${end_unix}" '+%Y-%m-%dT%H:%M:%S.%3NZ')
printf "%s" ${end_date}