减少日期的小时部分(不是24小时)?

时间:2017-04-27 04:45:42

标签: bash date unix compare hour

我的问题是缩短日期的小时部分。 我不允许使用日期命令只切!!!!! 假设那时的date命令读取 星期二4月26日17:07:49 PDT 2017 然后它应该打印出来 5:07

我遇到了很多麻烦,因为它没有识别对int的读取而是识别字符串。好久不见了我...

到目前为止,这是我的代码:

today=$(date)

echo The date right now is "$today"
echo "The time right now is $(printf '%s\n' "$today" | cut -c 11-19)"
let hour=${today:11:2}
let minute=${today:13:2}

if [[ ${hour} -ge 43200 ]]
        then
        let answer=hour-12

                echo "The correct time is $answer:$minute pm"
fi

我从中午12点的秒数计算出43200 目前的输出是:

现在的日期是2017年4月26日星期三21:47:39

现在的时间是21:47:39

1 个答案:

答案 0 :(得分:0)

let hour=${today:11:2}
let minute=${today:13:2}

您有以下字符串格式(带位置):

          1         2
0123456789012345678901234567
Wed Apr 26 21:47:39 PDT 2017
           ^ ^

这意味着偏移11处的两个字符是17(正确),但是在偏移13处提取分钟,即:0。分钟的正确提取将是${today:14:2}