2016-09-20 59 views
-2

我有以下內容: 秒,分鐘(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 
+4

這不是代碼寫入服務。特別是當你的問題看起來像是某種功課。 – Phylogenesis

回答

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