I print the start and end time using date +"%T"
, which results in something like:
10:33:56
10:36:10
How could I calculate and print the difference between these two?
I would like to get something like:
2m 14s
I print the start and end time using date +"%T"
, which results in something like:
10:33:56
10:36:10
How could I calculate and print the difference between these two?
I would like to get something like:
2m 14s
I realize this is an older post, but I came it across it today while working on a script that would take dates and times from a log file and compute the delta. The script below is certainly overkill, and I highly recommend checking my logic and maths.
You will get output as follows.
I modified the script a bit to make it standalone (ie. just set variable values), but maybe the general idea comes across as well. You'd might want some additional error checking for negative values.
I needed a time difference script for use with
mencoder
(its--endpos
is relative), and my solution is to call a Python script:fractions of seconds are also supported:
and it can tell you that the difference between 200 and 120 is 1h 20m:
and can convert any (probably fractional) number of seconds or minutes or hours to hh:mm:ss
timediff.py:
I can't comment on mcaleaa's answer, hence I post this here. The "diff" variable should be on small case. Here is an example.
Previous variable was declared on lower case. This is what happened when upper case is used.
So, quick fix would be like this
With GNU
units
:Here's some magic:
sed
replaces a:
with a formula to convert to 1/60. Then the time calculation that is made bybc
Following on from Daniel Kamil Kozar's answer, to show hours/minutes/seconds:
So the full script would be: