0

I grepped from the start time and end time strings from the program log. How can I find the time difference in hour?

t1="Tue Feb 21 12:15:00 HKT 2023"
t2="Tue Feb 21 12:45:01 HKT 2023"

I looked at other questions. But they have either different formats or directly from date like date -d 'now + 3 weeks'

roaima
  • 107,089
  • 14
  • 139
  • 261
SBMVNO
  • 103
  • 4

1 Answers1

0

Like this:

#!/bin/bash

export TZ='Asia/Hong_Kong'
oldd=$(date -d "Tue Feb 21 12:15:00 HKT 2023" "+%s")
last=$(date -d "Tue Feb 21 12:45:01 HKT 2023" "+%s")
diff=$((last - oldd))
echo "$((diff/3600 )) hours $(( (diff/60)%60 )) minutes $(( diff%60 )) seconds"

Output

0 hours 30 minutes 1 seconds
Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82