Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to know if there is a way to calculate the difference between two times in bash. I have two fields that are extracted from a log file:

Start time: Feb 12 10:02:10 End time: Feb 12 10:53:15

What I need is a manual way to get the elapsed time between these 2 dates. Since the time values are "text based", and not from a date generating command, I don't know if there's a way to do it. The times are 24 hour based, so hopefully that'll make times spanning midnight easier to handle. Any suggestions will be greatly appreciated. Peter V.

share|improve this question
See here for a number of solutions: unix.stackexchange.com/questions/1825/… – Steven D Feb 12 '11 at 17:32

You could use some evil trickery:

date -d "Feb 12 10:02:10" +%s

will return the number of seconds since the epoch for that date. Combining this with the same command (for the other date) and running through bc:

echo "`date -d 'Feb 12 10:53:15' +%s` - `date -d 'Feb 12 10:02:10' +%s`" | bc

Will give you


the number of seconds between the times. You could probably parse this out with awk somehow if you needed to run it more than once.

share|improve this answer
great solution. – Tim Kennedy Feb 12 '11 at 16:55
Exactly what I would have done. Looks like it depends on GNU date though; see the question Steven D linked to. – Jander Feb 12 '11 at 20:20

If you don't mind an external helper utility, have a look at my dateutils. Above task translates to:

ddiff -i '%b %d %H:%M:%S' 'Feb 12 10:02:10' 'Feb 12 10:53:15'

where -i is used to describe the input date format.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.