?
Solved

Difference of two dates/times

Posted on 2003-03-31
5
Medium Priority
?
858 Views
Last Modified: 2013-12-26
Hi,
I am writing a shall script wherein I need the difference between two dates and times. Say
DATE1=1st April, 2003 04:15:25
DATE2=3rd April, 2004 17:45:55

I need to calculate the difference of DATE1 and DATE2 (DATE2-DATE1) in total number of hours/minutes/seconds. Can you please suggest some function/shell command to achieve the same?
Is there any free downloadable script to achieve the same?

Please suggest

Thanks in advance

Rgds,
Gagan Gandhi
0
Comment
Question by:gagangandhi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Accepted Solution

by:
rob-g earned 750 total points
ID: 8238451
Try the Korn script below:

You'll need to change the way that the variables y1, m1, d1, h1, i1, s1, and y2, m2, d2, h2, i2, s2 are set up. Here the values are hard coded.

==================================================
#!/bin/ksh
# Display the difference between two dates in terms of hours, minutes & seconds.

function jds {
y=${1}
m=${2}
d=${3}
let jdn1=0+(1461*(y+4800+(m-14)/12))/4+(367*(m-2-12*((m-14)/12)))/12
let j=0+(3*((y+4900+(m-14)/12)/100))/4
let jdn1=jdn1-j+d-32075
echo $jdn1
}

echo "2003 04 01 04 15 25"|read y1 m1 d1 h1 i1 s1
echo "2004 04 03 17 45 55"|read y2 m2 d2 h2 i2 s2

days1=`jds $y1 $m1 $d1`
days2=`jds $y2 $m2 $d2`

secs1=$(( (days1*86400)+(h1*3600)+(i1*60)+(s1) ))
secs2=$(( (days2*86400)+(h2*3600)+(i2*60)+(s2) ))

secondsDiff=$((secs2 - secs1))

hdiff=$(( secondsDiff/3600 ))
mdiff=$(( (secondsDiff-(hdiff*3600))/60 ))
sdiff=$(( (secondsDiff-((hdiff*3600)+(mdiff*60))) ))

[[ ${hdiff} -eq 1 ]] && { echo "${hdiff} hour, \c"; }
[[ ${hdiff} -gt 1 ]] && { echo "${hdiff} hours, \c"; }
[[ ${mdiff} -eq 1 ]] && { echo "${mdiff} minute \c"; }
[[ ${mdiff} -gt 1 ]] && { echo "${mdiff} minutes \c"; }
[[ ${sdiff} -eq 1 ]] && { echo "${sdiff} second"; }
[[ ${sdiff} -gt 1 ]] && { echo "${sdiff} seconds"; }
==================================================
0
 
LVL 3

Expert Comment

by:prady_21
ID: 8242514
#!/bin/csh

set date1="2003-01-02 09:00:01";
set valdate=`date --date="${date1}" +%s`

set date2="2003-01-03 09:00:01";
set valdate2=`date --date="${date2}" +%s`


set endresult = `expr $valdate2 - $valdate`;
#set endresult = `expr $endresult / 86400`
echo $endresult;


This program gives the no of days between the two dates
You can add your own functions to get hours min and secs. It is not difficult, just do the division.

I am finding the no of seconds between the two dates.
In the end, when echo displays endresult, it will display the no of seconds between the two dates


Hope that helps
0
 

Expert Comment

by:juantamad
ID: 8243866
i hope this help... it belongs from an old script of mine, it's quite long but it checks all possibilities leap year and between months. it does not include seconds and it would not be hard to add it.

==================================================
#!/bin/sh

let c1_day=1
let c1_month=4
let c1_year=2003
let c1_hour=4
let c1_minute=15
         
let c2_day=3
let c2_month=4
let c2_year=2004
let c2_hour=17
let c2_minute=45
         
let c2_time=${c2_hour}*60+${c2_minute}
let c1_time=${c1_hour}*60+${c1_minute}
         
if test $c1_time -gt $c2_time
then
     let c2_time=${c2_time}+1440
     let c2_day=${c2_day}-1
fi
     
let c_time=${c2_time}-${c1_time}
let c_hour=${c_time}/60
let c_minute=${c_time}%60
         
until test $c2_day -ge $c1_day
do
     case $c2_month in
     
     1|3|5|7|8|10|12) let c2_day=${c2_day}+31 ;;
     4|6|9|11) let c2_day=${c2_day}+30 ;;
     2) let leap=$r_year%4
       
        if test $leap -ne 0
        then
          let c2_day=${c2_day}+28
        else
           let c2_day=${c2_day}+29
        fi ;;
     
      esac
     
      let c2_month=${c2_month}-1
               
      if test c2_month -eq 0
      then
           let c2_month=12
          let c2_year=${c2_year}-1
      fi
done
         
let c_day=${c2_day}-${c1_day}

if test $c2_month -lt $c1_month
then
     let c2_month=${c2_month}+12
     let c2_year=${c2_year}-1
fi
         
let c_month=${c2_month}-${c1_month}
     
let c_year=${c2_year}-${c1_year}

echo          
echo "The difference is $c_year year(s) $c_month month(s) $c_day day(s) $c_hour hour(s) $c_minute minute(s)"
echo
===============================================

hope this helps. :P
         

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8328206
AFAIK there is no bullet proof solution with shell scripting, you get couhgt in a trap somehow, sometimes, allways, depending on the dates.
I's suggest using perl with Date::Manip module.
0
 
LVL 18

Expert Comment

by:liddler
ID: 9846065
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by rob-g

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question