How to Compare Two Datetime Strings and Return Difference in Hours? (Bash Shell)

How to compare two DateTime strings and return difference in hours? (bash shell)

You could use date command to achieve this. man date will provide you with more details. A bash script could be something on these lines (seems to work fine on Ubuntu 10.04 bash 4.1.5):

#!/bin/bash                                                                                                                                                   

# Date 1
dt1="2011-11-11 11:11:11"
# Compute the seconds since epoch for date 1
t1=$(date --date="$dt1" +%s)

# Date 2 : Current date
dt2=$(date +%Y-%m-%d\ %H:%M:%S)
# Compute the seconds since epoch for date 2
t2=$(date --date="$dt2" +%s)

# Compute the difference in dates in seconds
let "tDiff=$t2-$t1"
# Compute the approximate hour difference
let "hDiff=$tDiff/3600"

echo "Approx hour diff b/w $dt1 & $dt2 = $hDiff"

Hope this helps!

Shell: How to compare two Time strings and return difference in minutes?

Convert your times to epoch and calculate seconds. I assume that you don't get your times from date because if so you can just format with "+%s" to get the epoch time immediately. The answer assumes you just have two strings with %H%M.

So, assign your strings with the timestamp (hour and minute)

current_utc_time=$(date +"%H%M")
sch_time=$(date +"18%M")

Convert to unix timestamp.

macOS

epoch_current=$(date -j -f "%H%M" "$current_utc_time" +%s)
epoch_sch=$(date -j -f "%H%M" "$sch_time" +%s)

Linux

epoch_current=$(date --date "$current_utc_time" +%s)
epoch_sch=$(date --date "$sch_time" +%s)

Calculate your diff (in minutes).

diff_in_minutes=$(( ($epoch_sch - $epoch_current) / 60 ))

Compare Date-Time Stamps

To just find the newer (or older) of the two timestamps, you could just use a string comparison operator:

time1="2013-12-10 13:25:30.123"
time2="2013-12-10 13:25:31.123"

if [ "$time1" > "$time2" ]; then
echo "the 2nd timestamp is newer"
else
echo "the 1st timestamp is newer"
fi

And, to find the time difference (tested):

ns1=$(date --date "$time1" +%s%N)
ns2=$(date --date "$time2" +%s%N)
echo "the difference in seconds is:" `bc <<< "scale=3; ($ns2 - $ns1) / 1000000000"` "seconds"

Which, in your case prints

the difference in seconds is: 1.000 seconds

Compare two dates in shell script

You may need to call out to expr, depending on your mystery shell:

d1="2015-03-31" d2="2015-04-01"
if [ "$d1" = "$d2" ]; then
echo "same day"
elif expr "$d1" "<" "$d2" >/dev/null; then
echo "d1 is earlier than d2"
else
echo "d1 is later than d2"
fi
d1 is earlier than d2

The test command (or it's alias [) only implements string equality and inequality operators. When you give the (non-bash) shell this command:

[ "$d1" > "$d2" ]

the > "$d2" part is treated as stdout redirection. A zero-length file named (in this case) "2015-04-01" is created, and the conditional command becomes

[ "$d1" ]

and as the variable is non-empty, that evaluates to a success status.

The file is zero size because the [ command generates no standard output.

Find difference between two dates in bash

You can use:

date1="Sat Dec 28 03:22:19 2013"
date2="Sun Dec 29 02:22:19 2013"
date -d @$(( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) )) -u +'%H:%M:%S'

And the output is:

23:00:00

However, for arbitrary differences over 24 hours, you have to start worrying about how the value for days will be represented. Negative values will also present problems.

The problem with this is that if you drop the format string, the date presented is:

Thu Jan  1 23:00:00 UTC 1970

So, while this works for the two given date/time values, it is not really a general solution.

Consider adding %j for the day of year; it will work semi-sanely for up to 1 year's difference.

Otherwise, capture the difference in a variable and present the results with a separate calculation.

date1="Sat Dec 28 03:22:19 2013"
date2="Sun Dec 29 02:22:19 2013"
delta=$(( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) ))
if [[ $delta -lt 0 ]]
then sign="-"; delta=${delta/^-/}
else sign="+"
fi
ss=$(( $delta % 60 ))
delta=$(( $delta / 60 ))
mm=$(( $delta % 60 ))
delta=$(( delta / 60 ))
hh=$(( $delta % 24 ))
dd=$(( $delta / 24 ))
printf "$sign%d %.2d:%.2d:%.2d\n" $dd $hh $mm $ss

Output:

+0 23:00:00

(Meaning: a positive difference of 0 days, 23 hours, 0 minutes, 0 seconds.) Splitting a number of days such as 1000 into years, months and days is fraught without any reference dates. Here, there are reference dates to help, but it would still be ... fun.



Related Topics



Leave a reply



Submit