This question already has an answer here:

I am writing a script to kill a job which runs more than 30 minutes. My epoch time format is like 1503935475067 and I am using current_time_stamp=`date +%s%3N`

How to find difference between two epoch time stamps?

marked as duplicate by DopeGhoti, Kusalananda, Community Aug 28 '17 at 17:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Are you asking how to do an arithmetic substraction? Is the problem that your shell (or the tools you generally use for arithmetic) only supports 32bit integers? – Stéphane Chazelas Aug 28 '17 at 16:11
  • Or is it that you want the difference in number of UTC seconds as opposed to Unix seconds (86400th part of a day; varying in duration)? – Stéphane Chazelas Aug 28 '17 at 16:13
  • Or is it that you want it in a [[d-]hh:]mm:ss.mmm format or other? – Stéphane Chazelas Aug 28 '17 at 16:21

With bash using double parenthesis:

date1=$(date +%s%3N)

if  (( ($date1-$date2)/60000 > 30 )); then
    # do something
  • 1
    Note that that syntax originated in ksh. You can also use (( (date1-date2)/60000 > 30 )) (also note that those timestamps are in miliseconds). That's actually better for cases where date2 is negative as the 123--234 would cause an error in some shell because of that improper use of the decrement operator. – Stéphane Chazelas Aug 28 '17 at 16:18
  • Thanks for the answer if i am trying to store the difference in a variable its throwing an error i tried in two ways like this time_diff=(( (date1-date2)/60000 )) and time_diff=(( (date1-date2)/60000 )) syntax error near unexpected token (' time_diff=(($date1-$date2)/60)' – Rahul Aug 28 '17 at 16:40
  • Use time_diff=$(( ($date1-$date2)/60000 )) – sebasth Aug 28 '17 at 16:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.