# Time Stap Diff using Bash

**URL:** <https://community.unix.com/t/time-stap-diff-using-bash/330730>\
**Category:** Shell Programming and Scripting\
**Created:** [June 19, 2013, 4:52am UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730 "2013-06-19T04:52:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![teefa](https://community.unix.com/letter_avatar/teefa/32/6_449c533f586939b6e5f246632a46421f.png) [@teefa](https://community.unix.com/u/teefa)\
**Post date:** [June 19, 2013, 4:52am UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/1 "2013-06-19T04:52:12Z")

</div>

hi all

kindly i want to subtract to time stamps

like:

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

```

using bash  
thanks alot

---

<div class="post-metadata">

**Author:** ![rajamadhavan](https://community.unix.com/letter_avatar/rajamadhavan/32/6_449c533f586939b6e5f246632a46421f.png) [@rajamadhavan](https://community.unix.com/u/rajamadhavan)\
**Post date:** [June 19, 2013, 5:22am UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/2 "2013-06-19T05:22:14Z")

</div>

something like this

```nohighlight
$ 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

---

<div class="post-metadata">

**Author:** ![pravin27](https://community.unix.com/letter_avatar/pravin27/32/6_449c533f586939b6e5f246632a46421f.png) [@pravin27](https://community.unix.com/u/pravin27)\
**Post date:** [June 19, 2013, 5:38am UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/3 "2013-06-19T05:38:07Z")

</div>

```nohighlight
 
#!/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"
 

```

---

<div class="post-metadata">

**Author:** ![teefa](https://community.unix.com/letter_avatar/teefa/32/6_449c533f586939b6e5f246632a46421f.png) [@teefa](https://community.unix.com/u/teefa)\
**Post date:** [June 19, 2013, 9:19am UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/4 "2013-06-19T09:19:58Z")

</div>

> [@rajamadhavan](#):
>
> something like this
> 
> ```plaintext
> $ 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

this is the output kindly help

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

```

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

> [@pravin27](#):
>
> ```plaintext
>  
> #!/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

```plaintext
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

```

---

<div class="post-metadata">

**Author:** ![in2nix4life](https://community.unix.com/user_avatar/community.unix.com/in2nix4life/32/709_2.png) [@in2nix4life](https://community.unix.com/u/in2nix4life)\
**Post date:** [June 19, 2013, 9:44am UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/5 "2013-06-19T09:44:41Z")

</div>

```nohighlight
#!/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.

```

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [June 19, 2013, 9:51am UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/6 "2013-06-19T09:51:15Z")

</div>

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

```plaintext
uname -a

```

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

I would recommend referring this [thread](http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html) in FAQ section.

---

<div class="post-metadata">

**Author:** ![teefa](https://community.unix.com/letter_avatar/teefa/32/6_449c533f586939b6e5f246632a46421f.png) [@teefa](https://community.unix.com/u/teefa)\
**Post date:** [June 19, 2013, 2:06pm UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/7 "2013-06-19T14:06:03Z")

</div>

> [@yoda](#):
>
> Going forward please provide details regarding which system that you are working on:
> 
> ```plaintext
> uname -a
> 
> ```
> 
> The solution posted by pravin27 & in2nix4life is assuming you have GNU date.
> 
> I would recommend referring this [thread](http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html) in FAQ section.

its

```plaintext
sun 11

```

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

> [@in2nix4life](#):
>
> ```plaintext
> #!/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.
>  
> 
> ```

```plaintext
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 .
 

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 19, 2013, 2:54pm UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/8 "2013-06-19T14:54:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [June 19, 2013, 3:15pm UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/9 "2013-06-19T15:15:02Z")

</div>

Here is the [date arithmetic](http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html) section of our FAQ with plenty of resources for you.

---

<div class="post-metadata">

**Author:** ![teefa](https://community.unix.com/letter_avatar/teefa/32/6_449c533f586939b6e5f246632a46421f.png) [@teefa](https://community.unix.com/u/teefa)\
**Post date:** [June 20, 2013, 2:32am UTC](https://community.unix.com/t/time-stap-diff-using-bash/330730/10 "2013-06-20T02:32:26Z")

</div>

i ve tried this thanks alot to you all :

```nohighlight
 
 
#! /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
 

```
