Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-30-2008, 06:56 AM   #1
LQ Newbie
Registered: Dec 2008
Posts: 3

Rep: Reputation: 0
Smile how to get difference between two times

STARTING 06:12:23

SUCCESS 06:19:41

how to get diff of two times
Old 12-30-2008, 07:05 AM   #2
LQ Newbie
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
5:34:06 5:34:14 0:00:08
Old 12-30-2008, 07:05 AM   #3
LQ Newbie
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
can someone do me a favor for the above
Old 12-30-2008, 07:32 AM   #4
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
My advice would be to (1) convert your times (as you get them) to epoch time and (2) then take the difference of those two. You can then (3) convert the difference into a more human-friendly time format whenever you need.

Example (using your first request):
06:12:23 or epoch time: 1230617543

06:19:41 or epoch time: 1230617981

438 seconds difference or 7 minutes and 18 seconds
You will need to write a program to take in the two timestamps and do the computations, if you plan to do this regularly. Here are a few links to sites that can show you what's involved.

The first website has short, but helpful discussions about how to compute epoch time from "human" formats and vice versa in various languages.

Edit: I'm assuming that you want help doing this with a program and not that you just want us to do the math for you.

Last edited by Telemachos; 12-30-2008 at 07:47 AM.
Old 12-30-2008, 07:41 AM   #5
Registered: Sep 2005
Location: UK
Distribution: Gentoo/Debian/Ubuntu
Posts: 307

Rep: Reputation: 38
Or use nano seconds -
date +%N
Old 12-30-2008, 08:01 AM   #6
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You have also to check if the END time is smaller or greater than the START time. For example if a job starts at 23:45:09 and terminates at 00:23:18 you have to take in account the change of day in your calculations (to not mention the troubles introduced by the passage from/to the Daylight Saving Time).

Anyway, here is an example of what you can do:

if [[ $END < $START ]]
  elapsed=$(date -d "00:00:00 $(($(date -d "19700102 $END" +%s) - \
          $(date -d "19700101 $START" +%s))) seconds" +%H:%M:%S)
  elapsed=$(date -d "00:00:00 $(($(date -d "19700101 $END" +%s) - \
          $(date -d "19700101 $START" +%s))) seconds" +%H:%M:%S)

echo $elapsed
END and START must be in HH:MM:SS format to let the above code work, since the comparison is done by ASCII order (string comparison).

Last edited by colucix; 12-30-2008 at 08:03 AM.
Old 12-30-2008, 10:21 AM   #7
Senior Member
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Originally Posted by prasanta_das View Post
how to get diff of two times
Here's a script that I posted as an answer to a similar question in another thread.

This script will show you how to convert a time to seconds since the epoch. You can then compare the seconds.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 05:26 AM
IMAP Times out!!! halo_75 Linux - Networking 3 05-30-2007 08:48 AM
Data transfer online is slow "at times" or "stops at times" Balarabay1 SUSE / openSUSE 14 04-30-2006 11:00 AM
how to use times(2) ... sachitha Programming 6 01-31-2006 09:23 PM
System hangs; Atheros Madwifi-ping times out every 15/16 times james 456 Linux - Networking 0 01-12-2006 07:55 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:58 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration