Difference between two dates
Hi all,
I want to find the diffrenece between two dates. for eg: time1: Tue Feb 14 14:43:12 IST 2006 time2: Tue Feb 14 15:50:09 IST 2006 Now I want the difference between time1&time2.(for eg:67min15seconds). I need some logic for the above scenario in shell script. It is very urgent.Can you please help me. |
Convert both dates into Julian format (or timestamp), that is a fixed number of seconds from a given reference point, subtract them to get the difference in seconds and then convert that to the hours minutes second format that you desire
|
look here at the perl cookbook,
you can't do it using shell script. (well I can't anyway!) http://www.unix.org.ua/orelly/perl/index.htm |
Quote:
Let me know if you find answers for this.I am also looking for same kind of script |
Just divide the timestamp difference by $((60*60*24)) for the number of day's between two times. Your times are less than a day:
tdate1=$(date -d 'Tue Feb 14 15:50:09 IST 2006' +%s) tdate2=$(date -d 'Tue Feb 14 14:43:12 IST 2006' +%s) echo $tdate1, $tdate2 1139912409, 1139908392 echo $(( ($tdate1-$tdate2) / ( 60*60*24) )) 0 tdate2=$(date -d 'Tue Feb 14 14:43:12 IST 2006' +%s) echo $(( ($tdate1-$tdate2) / ( 60) )) 66 echo $(( ($tdate1-$tdate2) / ( 60) )) minutes, $(( ($tdate1-$tdate2) % 60 )) seconds 66 minutes, 57 seconds --- P.S. Did you read the man page for the date command? Due to leap seconds, a delta time difference across a year boundary may not be reliable (depending on the time standard used). Wikipedia has an explanation. Also a mystery. The tidal effect slows down the earth's rotation by about a second per year. However, in the last few years, the earth's rotation has been speeding up. |
When working with time differences, time stamps is the way to go, like jschiwal proposed.
However, converting to HH:MM:SS is easier with date (again!) Code:
date -d "1970-01-01 + 145 seconds" +%T jlinkels |
All times are GMT -5. The time now is 04:36 AM. |