nightfury
nightfury

Reputation: 412

Time difference in seconds between given two dates

I have two dates as follows:

2019-01-06 00:02:10 | END
2019-01-05 23:52:00 | START

How could I calculate and print the difference between START and END dates in seconds?

For above case I would like to get something like:

610

Upvotes: 0

Views: 1075

Answers (5)

dpzz
dpzz

Reputation: 121

Was trying to solve the same problem on a non-GNU OS, i.e. macOS. I couldn't apply any of the solutions above, although it inspired me to come up with the following solution. I am using some in-line Ruby from within my shell script, which should work out of the box on macOS.

START="2019-01-05 23:52:00"
END="2019-01-06 00:02:10"

SECONDS=$(ruby << RUBY
require 'date'
puts ((DateTime.parse('${END}') - DateTime.parse('${START}')) * 60 * 60 * 24).to_i
RUBY)

echo ${SECONDS}
# 610

Upvotes: 1

Darby_Crash
Darby_Crash

Reputation: 446

START="2019-01-05 23:52:00"
END="2019-01-06 00:02:10"
parse () {
    local data=(`grep -oP '\d+' <<< "$1"`)
    local y=$((${data[0]}*12*30*24*60*60))
    local m=$((${data[1]}*30*24*60*60))
    local d=$((${data[2]}*24*60*60))
    local h=$((${data[3]}*60*60))
    local mm=$((${data[4]}*60))
    echo $((y+m+d+h+mm+${data[5]}))
}
START=$(parse "$START")
END=$(parse "$END")
echo $((END-START)) // OUTPUT: 610

Upvotes: 1

ashish_k
ashish_k

Reputation: 1571

Assuming GNU implementation based OS, you can use date's option %s and -d to calculate the time difference in seconds using command substitution and arithmetic operations.

START="2019-01-05 23:52:00"
END="2019-01-06 00:02:10"

Time_diff_in_secs=$(($(date -d "$END" +%s) - $(date -d "$START" +%s)))
echo $Time_diff_in_secs

Output:

610

Hope this helps!!!

Upvotes: 2

ghoti
ghoti

Reputation: 46826

What you're asking for is difficult verging on impossible using pure bash. Bash doesn't have any date functions of its own. For date processing, most recommendations you'll get will be to use your operating system's date command, but the usage of this command varies by operating system.

In BSD (including macOS):

start="2019-01-05 23:52:00"; end="2019-01-06 00:02:10"
printf '%d\n' $(( $(date -j -f '%F %T' "$end" '+%s') - $(date -j -f '%F %T' "$start" '+%s') ))

In Linux, or anything using GNU date (possibly also Cygwin):

printf '%d\n' $(( $(date -d "$end" '+%s') - $(date -d "$start" '+%s') ))

And just for the fun of it, if you can't (or would prefer not to) use date for some reason, you might be able to get away with gawk:

gawk 'END{ print mktime(gensub(/[^0-9]/," ","g",end)) - mktime(gensub(/[^0-9]/," ","g",start)) }' start="$start" end="$end" /dev/null

The mktime() option parses a date string in almost exactly the format you're providing, making the math easy.

Upvotes: 1

Cyrus
Cyrus

Reputation: 88553

With bash and GNU date:

while read d t x x; do
  [[ $x == "END" ]] && end="$d $t" 
  [[ $x == "START" ]] && start="$d $t"
done < file

end=$(date -u -d "$end" '+%s')
start=$(date -u -d "$start" '+%s')

diff=$(($end-$start))
echo "$diff"

Output:

610

See: man date

Upvotes: 2

Related Questions