Time Stap Diff using Bash

hi all

kindly i want to subtract to time stamps

like:

t1=08:35:20
t2=08:36:58
 
diff=00:01:38  <<<<
or
diff=01:38   <<<<

using bash
thanks alot

something like this

$ diffdate() { echo $(($(date -j -f "%H:%M:%S" "$2" "+%s") - $(date -j -f "%H:%M:%S" "$1" "+%s"))); }
$ diffdate "08:35:20" "08:36:58" | awk '{print int($1/60) ":" $1%60}'
1:38

Since you are not specifying the date, this will give incorrect results if you are trying to subtract timestamps rolling over to next date

 
#!/bin/ksh
t1=08:35:20
t2=13:36:58
today=$(date '+%Y-%m-%d')
EndSec=$(date  -d"$today $t2" '+%s')
StartSec=$(date  -d"$today $t1" '+%s')
Diff=$(expr $EndSec - $StartSec )
Sec=$(expr $Diff % 60 );Rem=$(expr $Diff / 60 );
min=$(expr $Rem % 60 ) ; Hrs=$(expr $Rem / 60 )
[ -z "$Hrs" ] && Hrs=0; [ -z "$Sec" ] && Sec=0; [ -z "$min" ] && min=0

echo "Diff is $Hrs:$min:$Sec"
 

this is the output kindly help

A: syntax error at line 1: `(' unexpected

---------- Post updated at 08:19 AM ---------- Previous update was at 08:19 AM ----------

this is the output kindly help

date: illegal option -- d
date: illegal option -- 2
date: illegal option -- 0
date: illegal option -- 1
date: illegal option -- 3
date: illegal option -- -
date: illegal option -- 0
date: illegal option -- 6
date: illegal option -- -
date: illegal option -- 1
date: illegal option -- 9
date: illegal option --  
date: illegal option -- 1
date: illegal option -- 3
date: illegal option -- :
date: illegal option -- 3
date: illegal option -- 6
date: illegal option -- :
date: illegal option -- 5
date: illegal option -- 8
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
date: illegal option -- d
date: illegal option -- 2
date: illegal option -- 0
date: illegal option -- 1
date: illegal option -- 3
date: illegal option -- -
date: illegal option -- 0
date: illegal option -- 6
date: illegal option -- -
date: illegal option -- 1
date: illegal option -- 9
date: illegal option --  
date: illegal option -- 0
date: illegal option -- 8
date: illegal option -- :
date: illegal option -- 3
date: illegal option -- 5
date: illegal option -- :
date: illegal option -- 2
date: illegal option -- 0
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
#!/bin/bash
#
#
#

# define times to compare
t1="08:35:20"
t2="08:36:58"

# convert to seconds since epoch
t1_c=$(date --date=$t1 +%s)
t2_c=$(date --date=$t2 +%s)

# subtract the seconds
diff=$(echo "scale=2; $t2_c - $t1_c" | bc)

# convert the diff back into normal time
# showing the difference between the two
# times
the_diff=$(date -d @+$diff +%M:%S)
echo "Time difference for $t1 and $t2 is $the_diff."

# done
exit 0

./subDATE.sh
Time difference for 08:35:20 and 08:36:58 is 01:38.

Going forward please provide details regarding which system that you are working on:

uname -a

The solution posted by pravin27 & in2nix4life is assuming you have GNU date.

I would recommend referring this thread in FAQ section.

its

sun 11

---------- Post updated at 01:06 PM ---------- Previous update was at 01:03 PM ----------

date: illegal option -- date=08:35:20
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
date: illegal option -- date=08:36:58
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
syntax error on line 1, teletype
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
Time difference for 08:35:20 and 08:36:58 is .
 

All these fancy options that easily convert dates are GNU options, usually only available on Linux. You don't have them on Sun.

There's a date math script here I'm hunting for.

Here is the date arithmetic section of our FAQ with plenty of resources for you.

i ve tried this thanks alot to you all :

 
 
#! /usr/bin/ksh
echo enter first time stamp
read TIME1
echo enter second time stamp
read TIME2
H1=`echo $TIME1 | cut -d\: -f1`
M1=`echo $TIME1 | cut -d\: -f2`
H2=`echo $TIME2 | cut -d\: -f1`
M2=`echo $TIME2 | cut -d\: -f2`
S1=`echo $TIME1 | cut -d\: -f3`
S2=`echo $TIME2 | cut -d\: -f3`
#echo $H1 $M1 $S1 $H2 $M2 $S2
        if [ $H1 -eq $H2 ]
        then
                ((MA1=M1*60+S1))
                ((MA2=M2*60+S2))
                ((MA1>MA2)) && ((MA2=MA2+86400))
                ((diff=MA2-MA1))
 
                                if [ $diff -lt 10 ]
                                then
                                        diff="0"$diff
                                fi
 
                echo $diff
        else
                ((H1>H2)) && ((H2=H2+24))
                ((diff=H2-H1))
                ((MA1=M1*60+S1))
                ((MA2=M2*60+S2))
                ((MA1>MA2)) && ((MA2=MA2+86400))
                ((diff2=MA2-MA1))
                ((diff2=diff2+diff*3600))
                                                        if [ $diff2 -lt 10 ]
                                                        then
                                                                diff2="0"$diff2
                                                        fi
                echo $diff2
        fi