-1

I would like to get a simple shell script to find the difference in two times.

Example:

Tue May 9 10:38:17 BST 2017

-rw-rw-r-- 1 unikix unikix 1387 Feb 17 11:34 ABC

The first one is the system date and other one, the time extracted from output of list command.

There is a need to check the time format also (12 hour clock and 24 clock) before finding the difference.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Praveen
  • 39
  • 1
  • 2
  • 7
  • Possible duplicate of [How to find the difference in days between two dates?](http://stackoverflow.com/questions/4946785/how-to-find-the-difference-in-days-between-two-dates) – Francis.Beauchamp May 09 '17 at 12:30

1 Answers1

0

Try the following:

DATE1="Tue May 9 10:38:17 BST 2017"
DATE2="Feb 17 11:34"

sec1=$(date --date "${DATE1}" +"%s")
sec2=$(date --date "${DATE2}" +"%s")

diff=$((${sec1} - ${sec2}))

echo "Difference is ${diff} sec"
Andriy Berestovskyy
  • 8,059
  • 3
  • 17
  • 33
  • Since, I am using AIX (an IBM version of unix), the option --date is not working. Please find the error below. Could you please help me in this. sec1=$(date --date "${DATE1}" +"%s") date: illegal option -- - Usage: date [-u] [+Field Descriptors] – Praveen May 09 '17 at 13:10