Date time difference in UNIX shell script

There are 2 dates,

Tue Oct  1 13:40:19 2013

Sun Sept 30 10:26:23 2013

I have multiple dates like the above one. How do I calculate the date time difference and display in another column in Shell script. Please help.

If your date command supports it, you can subtract the epoch of one from the other:

(not 100% tested, but something like...)

# E1=$(date '+%s' -d 'Tue Oct  1 13:40:19 2013')
# E2=$(date '+%s' -d 'Sun Sept 30 10:26:23 2013')
# echo $((E1 - E2))
98036
# DAYS=$(echo "$((E1-E2))/86400"|bc)
# HOURS=$(echo "$((E1-E2-86400*$DAYS))/3600"|bc)
# MINS=$(echo "$((E1-E2-86400*$DAYS-3600*$HOURS))/60"|bc)
# echo $DAYS $HOURS $MINS
1 3 13

(you can do this without bc, if you don't mind all the brackets...)

# DAYS=$(((E1-E2)/86400))
# HOURS=$((((E1-E2)-(86400*$DAYS))/3600))
# MINS=$((((E1-E2)-(86400*$DAYS)-(3600*$HOURS))/60))
# echo $DAYS $HOURS $MINS
1 3 13

Note: You should always state which OS you are using if it's not obvious from your question!

I am currently using HP-UX B.11.11 as my O/S.

---------- Post updated at 04:27 PM ---------- Previous update was at 04:23 PM ----------

And 1 more thing.

date -d

doesn't work in my HP-UX system.

There is no portable way to do it using POSIX shell scripting.

A portable solution would use a simple C code.

Otherwise, python, java, perl and others would allow to do it.