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'

7
  • Will the start and end always be on the same date? Feb 21, 2023 at 6:24
  • YES, since they are from the existing program log.
    – SBMVNO
    Feb 21, 2023 at 6:25
  • Does HKT jump forwards/backwards during the year? (It does in some countries, but not in others.) Feb 21, 2023 at 6:26
  • What have you tried so far? Feb 21, 2023 at 6:26
  • I think HKT means HongKongTime. I never see others.
    – SBMVNO
    Feb 21, 2023 at 6:27

1 Answer 1

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

Not the answer you're looking for? Browse other questions tagged .