LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Difference between two dates (http://www.linuxquestions.org/questions/programming-9/difference-between-two-dates-415242/)

Uday123 02-14-2006 05:51 AM

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.

graemef 02-14-2006 06:03 AM

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

bigearsbilly 02-14-2006 08:17 AM

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

BharathiNayak 02-23-2009 01:28 AM

Quote:

Originally Posted by Uday123 (Post 2103101)
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.


Let me know if you find answers for this.I am also looking for same kind of script

jschiwal 02-23-2009 02:26 AM

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.

jlinkels 02-23-2009 06:26 AM

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
yields 00:02:25

jlinkels


All times are GMT -5. The time now is 12:09 AM.