Difference between two date

Hi,
I created a script for finding the duration of a job using the start and end time of the job. But the command doesnt calculate correct value if the duration is more than 24 hours. Any help would be really good

.

cat test1    --- start time
03/27/15 17:41:00
03/24/15 11:58:04
03/23/15 06:00:00

cat test2    --- end time
03/27/15 17:51:30
03/24/15 18:14:01
03/24/15 07:00:00

Calculating difference using the below command
let DIFF=(`date +%s -d "$b"` - `date +%s -d "$a"`)   // calculating the duration in seconds
echo|date -u -d @${DIFF} +"%H"h" %M"m""  // converting into H:M format.

output

00h 10m   -- correct value
06h 15m   --  correct value
01h 00m   -- In correct value . Instead of 25h printing 1h as its not deducting the day from 24 to 23. 


date is giving an hour and minute for a timestamp. you'll need to calculate hours and minutes yourself.

mute@tiny:~$ a='03/23/15 06:00:00' b='03/24/15 07:00:00' diff=$(( $(date +%s -d "$b") - $(date +%s -d "$a") ))
mute@tiny:~$ printf '%dh %02dm\n' $((diff=diff/3600)) $((diff/60))
25h 00m
1 Like

Hi,
Thanks for your reply. The command is working fine for hours but it is not converting into minutes.

Used the below command and it works fine. Thanks a lot
printf '%dh %02dm\n' $((DIFF/3600)) $((DIFF%3600/60))

Try this instead:

$ printf '%dh %02dm\n' $((diff/3600)) $(((diff%3600)/60))