1. 程式人生 > >shell指令碼中計算時間差

shell指令碼中計算時間差

1. 獲取開始、結束時間(獲取當前時間)

timer_start=`date "+%Y-%m-%d %H:%M:%S"`
timer_end=`date "+%Y-%m-%d %H:%M:%S"`

2. 計算時間差

duration=`echo eval $(($(date +%s -d "${timer_end}") - $(date +%s -d "${timer_start}"))) | awk '{t=split("60 s 60 m 24 h 999 d",a);for(n=1;n<t;n+=2){if($1==0)break;s=$1%a[n]a[n+1]s;$1=int($1/a[n])}print s}'
` echo "耗時: $duration"

3. 執行結果

開始: 2018-09-04 14:52:19
結束: 2018-09-04 14:52:22
耗時: 5h39m3s