LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to Calculate Time Difference? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-calculate-time-difference-4175486168/)

santosh0782 11-28-2013 11:08 AM

How to Calculate Time Difference?
 
Hi,

I want to calculate time difference, i tried below steps however getting some error:

let say.. current_time=22:18 and previous_time=22:01 .
i tried as diff=$(($current_time - $previous_time))
error: syntax error:operand expected(error token is "22:18 - 22:01"

could someone please help me to calculate the diff?

Thanks in advance :-)

druuna 11-28-2013 12:15 PM

Have a look at this:
Code:

#!/bin/bash
# usage: time.diff.sh mm:ss mm:ss
# output: difference in seconds

OIFS=$IFS
IFS=":"

startTime=($1)
endTime=($2)

IFS=$OIFS

stInSecs=$(( ${startTime[0]}*60 + ${startTime[1]}  ))
etInSecs=$(( ${endTime[0]}*60  + ${endTime[1]} ))

diffSecs=$(( $stInSecs - $etInSecs ))

echo "$1 - $2 --> $diffSecs seconds."

Example run:
Code:

$ ./time.diff.sh 22:18 22:01
22:18 - 22:01 --> 17 seconds.

Calculating with full dates is easier to do:
Code:

#!/bin/bash

startDate=$( date -d "nov 28 09:10:40" +%s )
endDate=$( date -d "nov 27 23:37:08" +%s )
diffSecs=$(( startDate - endDate ))
echo "Difference: $diffSecs seconds."

Example run:
Code:

$ ./full.date.sh
Difference: 34412 seconds.


haertig 11-28-2013 12:25 PM

If you are using "GNU date" (which would be what's installed on most Linux systems these days):

Code:

let DELTA_SECONDS=(`date +%s -d "1/2/2013 00:00:00"`-`date +%s -d "1/1/2013 00:00:00"`)
echo $DELTA_SECONDS

If you don't want to display the difference in seconds as the above does (you want minutes, or hours, or days instead), you can just divide the above result by 60, 3600, 86400, etc. to convert to whatever unit you need.

santosh0782 11-29-2013 12:36 AM

Issue Resolved
 
Followed the steps provided by you and its working fine..
Thanks a lot for all your help and time :-)

santosh0782 11-29-2013 01:30 AM

How to Calculate Time Difference? Reply to Thread
 
Hi,

as i need a time difference in min, i am doing this:

a=$(date -d "$curr_date $curr_time" +%M )
b=$(date -d "$fps_validated_date $fps_validated_time" +%M )
diffmin=$(( $a - $b ))

however i am getting below errors:
1. a=$(date -d "$curr_date $curr_time" +%M)
date: invalid date `29/11/13 07:15'

2. b=$(date -d "$fps_validated_date $fps_validated_time" +%M )
date: invalid date `29/11/13 06:32'

how to resolve it?

druuna 11-29-2013 01:46 AM

Use a valid date.

This is a valid date: 2013/11/29 07:15 and this is not a valid date: 29/11/2013 07:15

Date should be in this format: YYYY/mm/dd HH:MM:SS

EDIT: The code in post #5 will probably not do what you expect it to do. Have a look at the following example:
Code:

#!/bin/bash

curr_date="2013/11/29"
curr_time="07:30"

fps_validated_date="2013/11/29"
fps_validated_time="06:30"

a=$(date -d "$curr_date $curr_time" +%M )
b=$(date -d "$fps_validated_date $fps_validated_time" +%M )

diffmin=$(( $a - $b ))

echo "Difference: $diffmin"

Example run:
Code:

$ ./foo.sh
Difference: 0

The trick used by haertig and me is translating the date into seconds from epoch (the %s) and calculate with that. Using %M will only show the minutes part of the date string.

santosh0782 11-29-2013 02:50 AM

How to Calculate Time Difference?
 
Actually there is a file with date as 29/11/13 07:15 and according to this i converted the system time in the similar format.
If i want to read this date 29/11/13 07:15 from a file and convert it into the format 2013/11/29 07:15, is there a way to convert it ? and then we can take a time difference?

druuna 11-29-2013 03:09 AM

Converting 29/11/13 07:15 into 2013/11/29 07:15:
Code:

$ foo="29/11/13 07:15"
$ echo $foo | awk -F"[ /]" '{ print "20"$3"/"$2"/"$1" "$4 }'
2013/11/29 07:15


hroptatyr 02-19-2014 06:01 AM

Using ddiff (from dateutils) makes calculating date and time differences easy:

Code:

$ ddiff 22:01 22:18
1020s
$ ddiff 22:01 22:18 -f '%M minutes'
17 minutes


santosh0782 02-22-2014 08:57 PM

Thanks a lot, its really good :-)


All times are GMT -5. The time now is 11:38 AM.