Get the no of hours between days

Hi,

i have a date 1- 2013101511
date2 -2013101812

need toget the no of hours between them,can any one tellme the logic.

Refer thread: Date Arithmetic

I'm supposing that the latest two digits rapresents the hour.

In bash:

date1='2013101511'
date2='2013101812'
ts_date1=$(date --date="${date1:0:8} ${date1:8}" '+%s')
ts_date2=$(date --date="${date2:0:8} ${date2:8}" '+%s')
let diff_hours=(ts_date2-ts_date1)/3600
echo $diff_hours

Emanuele

Note that --date only works on GNU date, usually not present anywhere but GNU/Linux. For date math that works anywhere, you'll likely need perl.

So, what's your system?

yes its GNU/Linux

---------- Post updated at 02:52 PM ---------- Previous update was at 02:50 PM ----------

yes thts correct the last two digits are hours.

Another approach using ksh93 builtin printf %T formatting option:

#!/bin/ksh93

date1="2013101511"
date2="2013101812"

SDATE=$( printf "%(%s)T" "${date1:0:8} ${date1:8:2}:00:00" )
EDATE=$( printf "%(%s)T" "${date2:0:8} ${date2:8:2}:00:00" )

printf "%d\n" "$(( ( $EDATE - $SDATE ) / 3600 ))"

thanks yoda..it worked as desired.