Difference in date time

前端 未结 3 482
梦毁少年i
梦毁少年i 2021-01-24 10:21

I have 2 variables.

GMDCOMTM which stores the date time Tue Oct  1 13:32:40 2013
GMDRRSTM which stores the date time Tue Oct  2 23:35:33 2013

H

3条回答
  •  遇见更好的自我
    2021-01-24 11:21

    I'm normal using a custom made function to do the calculations. It may be long way but it will definitely work on all UNIX and Linux based systems. Following the code block.

    time_diff(){
        foodate1=$1
        foodate2=$2
        foosecvall=`echo $foodate1 | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }'`
        foosecval2=`echo $foodate2 | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }'`
        foodiffsec=$((foosecvall-foosecval2));
    
        s=$foodiffsec
        h=$((s/3600));
        s=$((s-$((h*3600))));
        m=$((s/60));
        s=$((s-$((m*60))));
        footmstm=$h":"$m":"$s
    }
    

    Place the above code in your script and then call the function like follows.

    TIME1="10:12:14"
    TIME2="12:15:14"
    
    time_diff $TIME2 $TIME1
    
    echo $footmstm
    

提交回复
热议问题