Time stamp Difference

I have a log file which wrote time stamp like this

2013-02-11 00:46:40.389037 
 
         2013-02-12 11:46:40.197045

can any one help me to get the time stamp difference of these two line in seconds.

to=$(date -d "2013-02-12 11:46:40.197045" +%s)
from=$(date -d "2013-02-11 00:46:40.389037" +%s)
echo $(( $to - $from ))
1 Like

Hi, for calculare the difference between two date you must convert them in epoch with a code like this and then operate the difference

HOUR=12
MIN=41
SEC=11
DAY=01
MONTH=01
YEAR=2012
set -A MONTHS 0 0 31 59 90 120 151 181 212 243 273 304 334 365
echo "b=0;if(${MONTH}>2) if (${YEAR}%4==0) b=1; ${SEC}+${MIN}*60+${HOUR}*3600+(${MONTHS[${MONTH}]} + ${DAY} + b-1)*86400+(${YEAR}-1970)*31536000+((${YEAR}-1969)/4)*86400" | bc

thanks balajesuri ur code worked!

can you plz explain ur code.

# Calculate the time in seconds from epoch and store the value in "to". Check man date to see what switch -d and format %s do.
to=$(date -d "2013-02-12 11:46:40.197045" +%s)
from=$(date -d "2013-02-11 00:46:40.389037" +%s)

# Calculate the difference.
echo $(( $to - $from ))