GNU date is very useful for this:
Code:
date +%s -d "Fri May 11 12:34:56 2012"
date +%s -d "5 days ago"
date +%s
The output when using
+%s is the number of seconds since the Epoch, so you can use
$(( $(date +%s -d "later") - $(date +%s -d "earlier") )) in Bash to calculate the number of seconds between
later and
earlier.
Note that this will not work on all
date variants; it will only work on GNU
date, as far as I know.
To test if you have GNU date in Bash, you can use
Code:
if [ -n "date --version 2>/dev/null | grep -e GNU" ]; then
# Yes, you have GNU 'date'
else
# Oops, not a GNU 'date'!
fi