Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
YYYY-MM-DDTHH中的增量分钟数:MM:SS.SSSZ Bash脚本_Bash_Shell - Fatal编程技术网

YYYY-MM-DDTHH中的增量分钟数:MM:SS.SSSZ Bash脚本

YYYY-MM-DDTHH中的增量分钟数:MM:SS.SSSZ Bash脚本,bash,shell,Bash,Shell,我不知道我在哪里犯了错误。以下以指定格式向日期添加分钟的语法是否正确 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 "

我不知道我在哪里犯了错误。以下以指定格式向日期添加分钟的语法是否正确

        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

尝试使用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}

您可以完全避免子字符串操作:
date-d“$startDate+1分钟”+%Y-%m-%dT%H:%m:%S.%3NZ'
Z
应该是格林威治标准时间(在该时间,您应该使用+0000),还是您忘记了
%
%Z
扩展到本地时区,
Z
使
date
将时间戳视为+0000,因此它首先将其转换为本地时区。