我具有以下内容:秒(s),分钟(m),小时(h)和天(d),如下所述:并想通过从当前日期时间中减去给定时间将其转换为Date&Time。DATE1- DATE2 = DIFF,要通过提供DATE1和DIFF来计算DATE2。
Means if current time is 2016-04-02T12:00:00 and given input is 23H60M then output should be 2016-04-01T12:00:00, and it can be 23D24H,can be 23M20S...
400d - Only days (d) displayed
20d23h - days (d) and hours (h) displayed
14h33m - hours (h) and minutes (m) displayed
10m59s - minutes (m) and seconds (s) displayed
您可以尝试这样的事情;
#!/bin/bash
input=$(echo $1 | tr '[:lower:]' '[:upper:]')
if [[ $input == *"D"* ]]
then
input=$(echo $input | sed 's/D/ days ago /g')
fi
if [[ $input == *"H"* ]]
then
input=$(echo $input | sed 's/H/ hours ago /g')
fi
if [[ $input == *"M"* ]]
then
input=$(echo $input | sed 's/M/ minutes ago /g')
fi
if [[ $input == *"S"* ]]
then
input=$(echo $input | sed 's/S/ seconds ago /g')
fi
echo $input
CURRENTDATE="$(date +%Y-%m-%d-%H:%M:%S)"
echo $CURRENTDATE
OLDERDATE="$(date "+%Y-%m-%d-%H:%M:%S" -d "$input")"
echo $OLDERDATE
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句