subtracting Tue, Feb 26, 2008 01:38:25 AM from Mon, Feb 25, 2008 09:30:03 PM


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting subtracting Tue, Feb 26, 2008 01:38:25 AM from Mon, Feb 25, 2008 09:30:03 PM
# 15  
Old 03-23-2008
Just for completeness, here is a solution based on ksh93 version h or later.

Code:
#!/usr/bin/ksh93
#
# USAGE: diffdate start-date  finish-date
# 
# EXAMPLE: diffdate "Tue, Feb 19, 2008 08:00:02 PM" "Wed, Feb 20, 2008 02:19:09 AM"
#

SDATE=$(printf '%(%s)T' "$1")
FDATE=$(printf '%(%s)T' "$2")

[[ $# -ne 2 ]] && {
   print "Usage: diffdate start-date finish-date"
   exit 1
}

DIFF=$(($FDATE-$SDATE))

SECS=$(($DIFF % 60))
MINS=$(($DIFF % (60 * 60) / 60))
HOURS=$(($DIFF / (60 * 60)))

printf "%02d:%02d:%02d\n" $HOURS $MINS $SECS

exit 0

# 16  
Old 03-26-2008
Date difference

You can use the following :
It gives date difference in seconds.

T1='5:02:02'

T2='5:19:59'


h1=`echo $T1|cut -d: -f1`

m1=`echo $T1|cut -d: -f2`

s1=`echo $T1|cut -d: -f3`

x1=`echo "$h1*3600 + $m1*60 + $s1"|bc -l`


h2=`echo $T2|cut -d: -f1`

m2=`echo $T2|cut -d: -f2`

s2=`echo $T2|cut -d: -f3`

x2=`echo "$h2*3600 + $m2*60 + $s2"|bc -l`


if test $x1 -lt $x2

then

diff=`echo "$x2 - $x1"|bc -l`

else

diff=`echo "$x1 - $x2"|bc -l`

fi


echo "diff of Dates in seconds is $diff seconds"
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk mktime(strftime(format,"6-FEB-2013 08:50:03.841")

I'm trying to use AWK to filter on some dates in a field by converting them to Unix Time. mktime(strftime(format,"6-FEB-2013 08:50:03.841")What is the proper format for my date strings as they appear in my database? My first thought is %d-%b-%Y %H:%M:%Sbut I see the following issues: %d is... (3 Replies)
Discussion started by: Michael Stora
3 Replies

2. UNIX for Dummies Questions & Answers

converting date format: "May 31 2008" to "2008-05-31"

I have the following script to find out the last day of the last month .... and the output of this script is in the following format ... Script goes like this .... #!/bin/ksh cur_month=`date +%m` cur_year=`date +%Y` prev_month=$(($cur_month-1)) # Check to see if this is January if ... (8 Replies)
Discussion started by: santosham
8 Replies
Login or Register to Ask a Question