LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2006, 05:51 AM   #1
Uday123
Member
 
Registered: Nov 2005
Posts: 80

Rep: Reputation: 15
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.
 
Old 02-14-2006, 06:03 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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
 
Old 02-14-2006, 08:17 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
Old 02-23-2009, 01:28 AM   #4
BharathiNayak
LQ Newbie
 
Registered: Feb 2009
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by Uday123 View Post
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
 
Old 02-23-2009, 02:26 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 02-23-2009 at 02:32 AM.
 
Old 02-23-2009, 06:26 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
  


Reply



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
Subtracting dates chrisk5527 Linux - General 6 06-10-2004 02:57 PM
need to manipulate dates clsonnt Programming 5 08-19-2003 10:07 AM
problems with dates barkers Linux - General 1 04-30-2002 06:28 AM
Dates in PL/PGSQL anisbet Linux - General 0 11-19-2001 10:16 AM
dates on threads.... acid_kewpie LQ Suggestions & Feedback 1 10-26-2001 12:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 PM.

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