我有以下内容: 秒(s),分钟(m),小时(h)和天(d),如中所述 以下内容:并希望通过从当前日期时间减去给定时间将其转换为日期和时间。 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
答案 0 :(得分:1)
你可以尝试这样的事情;
#!/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