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
# 1  
Old 02-26-2008
subtracting Tue, Feb 26, 2008 01:38:25 AM from Mon, Feb 25, 2008 09:30:03 PM

Hi,

Please help me out in creating a script which will subtract Tue, Feb 26, 2008 01:38:25 AM from Mon, Feb 25, 2008 09:30:03 PM and will give me out put in the format hh:mm:ss.

I tried datecalc.It doesnt work for this format.
Sreejith_VK
# 2  
Old 02-26-2008
Code:
$ cat datediff.sh
#!/bin/sh

D1=`date +%s -d "Feb 26, 2008 01:38:25 AM"`
D2=`date +%s -d "Feb 25, 2008 09:30:03 PM"`
((diff_sec=D1-D2))
echo - | awk '{printf "%d:%d:%d","'"$diff_sec"'"/(60*60),"'"$diff_sec"'"%(60*60)/60,"'"$diff_sec"'"%60}'

$ sh datediff.sh
4:8:22

//Jadu
# 3  
Old 02-27-2008
The script is giving the error:

Quote:
date: bad format character - s
date: bad format character - s
./datediff.sh[5]: diff_sec=D1-D2: bad number
please help!!
Sreejith_VK
# 4  
Old 02-27-2008
Quote:
Originally Posted by Sreejith_VK
I tried datecalc.It doesnt work for this format.
Convert your date to the required format which datecalc expects and pass it.
# 5  
Old 02-27-2008
I thought you said the problem was solved. See Time Between Dates - The UNIX Forums

Please do not open multiple threads for the same problem. You already have opened 3 other threads. Break the rule once more and you will earn a temporary ban.
# 6  
Old 02-27-2008
The problem is not yet solved.
The output I require is in hh:mm:ss and dateclac doesn't provide me.
Sreejith_VK
# 7  
Old 02-27-2008
First check whether you can see the epoch time for the below command:

Code:
$ date +%s -d "Feb 26, 2008 01:38:25 AM"
1203970105

And if that is successful, then

try

Code:
diff_sec=`expr $D1 - $D2`

instead of

Code:
((diff_sec=D1-D2))

For me the script I gave above works.
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