Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Difference between two date values Post 302957624 by RavinderSingh13 on Tuesday 13th of October 2015 10:16:13 AM
Old 10-13-2015
Quote:
Originally Posted by sam@sam
Code:
bash-3.00$ date

Tue Oct 13 15:03:54 CEST 2015
Code:
start=`date +"%T" | awk -F":" '{print $2,$3}'`
echo $start

08 17
Code:
end=`date +"%T" | awk -F":" '{print $2,$3}'`
echo $end

08 37
how can I difference between above both times, Im trying but getting errors:
Code:
bash-3.00$ echo `expr $end - $start`

expr: syntax error
Code:
bash-3.00$ echo  $(($end - $start))

bash: 08: value too great for base (error token is "08")
expected output for this case should be something like 00 20
Hello sam@sam,

Could you please try following and let us know if this helps.
Code:
START=`date +"%T" | awk -F":" '{print $2 FS $3}'`
##### I ran following command after few mins
END=`date +"%T" | awk -F":" '{print $2 FS $3}'`
##### Then use following awk command to calculate difference.
awk -vstart=$START -vend=$END 'BEGIN{split(start, A,":");split(end, B,":");sec_start=A[1] * 60 + A[2];sec_end=B[1] * 60 + B[2];diff=sec_end - sec_start;print "Minutes \t Seconds" ORS int(diff/60) "\t\t " diff%60}'

You could use this in script too as per your convenience too.
NOTE: It is only looking for taking difference of minutes and seconds not dates.

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-13-2015 at 11:45 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
 

10 More Discussions You Might Find Interesting

1. Linux

date difference

hi, i have 2 dates in the form: '20080315120030' and '20080310140030'. i.e. YYYYMMDDHHMMSS. i need a way of getting the difference between them using shell script. any thoughts? (14 Replies)
Discussion started by: muay_tb
14 Replies

2. Shell Programming and Scripting

Bash script getting difference values from files

Hi I want to write a BASH script. I have files updated every hour from two platforms.The file names are : - Falcon_smsCounters_1200020708.log - Canari_smsCounters_1200020708.log 1200 refers to hour twelve and 020708 to 02 july 2008. Inside every file i have a list of parameters... (2 Replies)
Discussion started by: salimayoub
2 Replies

3. Shell Programming and Scripting

Korn Shell Variable values difference

I am using two shell scripts a.ksh and b.ksh a.ksh 1. Sets the value +++++++++++++++++ export USER1=abcd1 export PASSWORD=xyz +++++++++++++++++ b.ksh 2. Second scripts calls sctipt a.ksh and uses the values set in a.ksh and pass to an executable demo... (2 Replies)
Discussion started by: kunalseth
2 Replies

4. Shell Programming and Scripting

Calculate difference between two successive values

Hi, I have a file containing timestamps (at micro-seconds granularity). It looks like the following: 06:49:42.383818 06:49:42.390190 06:49:42.392308 06:49:42.392712 06:49:42.393437 06:49:42.393960 06:49:42.402115 Now I need a sed/awk script to take the difference of two successive... (2 Replies)
Discussion started by: sajal.bhatia
2 Replies

5. Shell Programming and Scripting

Difference in date

Dear all, I fancy that I'm pretty competent in ksh, but I have someone on HP-UX wanting me to script up a simple interface to handle user alterations rather than giving them high privileges to run up SAM. This is all fairly straightforward, but I'm stuck on an epoch date issue. When we have... (6 Replies)
Discussion started by: rbatte1
6 Replies

6. Homework & Coursework Questions

help with the date difference

1. The problem statement, all variables and given/known data: The problem i have is that i probably make a few mistake here in the code but don't know what it is and i try to get the date difference but don't know where to add the days_in_month function 2. Relevant commands, code,... (1 Reply)
Discussion started by: mgyeah
1 Replies

7. Programming

Date difference

I tried the below code to find difference between two dates. It works fine if the day of the month is 2-digit number. But it fails when we have a single-digit day of month(ex:1-9). my code is as below. please help me soon. #!/usr/bin/perl -w use strict; use Time::Local; ... (2 Replies)
Discussion started by: anandrec
2 Replies

8. Shell Programming and Scripting

Date difference

HI All , i need a bash script to find the number of days between two dates . Format YYYY-MM-DD THanks, Neil (1 Reply)
Discussion started by: nevil
1 Replies

9. AIX

Time Difference between date and date -u

Hi Everyone, We are having an issue with date and date -u in our AIX Systems. We have checked environment variable TZ and /etc/environment and however, we could not rectify the difference. >date Thu Mar 19 22:31:40 IST 2015 >date -u Thu Mar 19 17:01:44 GMT 2015 Any clue... (5 Replies)
Discussion started by: madhav.kunapa
5 Replies

10. UNIX for Beginners Questions & Answers

Changing CSV files with date . Subtracting date by values

Hi All, I have a CSV file which is as below. Basically I need to take the year column in it and find if the year is >= 20152 . If that is then I should subtract all values by 6. In the below example in description I am having number mentioned as YYWW so I need to subtract those by -5. Whereever... (8 Replies)
Discussion started by: arunkumar_mca
8 Replies
All times are GMT -4. The time now is 05:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy