• Status: Solved
• Priority: Medium
• Security: Public
• Views: 869

# Difference of two dates/times

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?

Rgds,
Gagan Gandhi
0
gagangandhi
1 Solution

Commented:
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

Commented:
#!/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

Commented:
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

Commented:
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

Commented:
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: