0

Can anyone help me to calculate the total minutes between below two dates.

date1= 2016-07-02 06:20:00

date2= 2016-07-04 15:00:00

the output should be in number for example the date diff is only 5 hours means i need the output like 5*60=300 minutes.

Thanks, AAA

  • 3
    http://stackoverflow.com/questions/14309032/bash-script-difference-in-minutes-between-two-times – P.... Nov 08 '16 at 09:39
  • Thanks PS. Its working fine which you redirected. sh-4.3$ #! /bin/bash sh-4.3$ currentdate=$(date -u +%s -d '2007-09-01 17:30:24') sh-4.3$ targetdate=$(date -u +%s -d '2007-12-25 12:30:00') sh-4.3$ minutes=$(( ($targetdate - $currentdate) / 60 )) sh-4.3$ echo $minutes 165299 Thanks a lot to all –  Nov 08 '16 at 11:31

1 Answers1

1

echo $((($(date -ud "$date2" +'%s') - $(date -ud "$date1" +'%s'))/60)) minutes  

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Sorin J
  • 542
  • 1
  • 4
  • 14
  • I have tried that but i am getting below error sh-4.3$ dtae1="2016-07-02 06:20:00" sh-4.3$ dtae2="2016-07-15 04:27:00" sh-4.3$ echo $((($(date -ud "$dtae2"+'%s')- $(date -ud "$dtae1"+'%s'))/60)) minutes date: invalid date '2016-07-15 04:27:00+%s' date: invalid date '2016-07-02 06:20:00+%s' sh: (- )/60: syntax error: operand expected (error token is ")/60") –  Nov 08 '16 at 11:02
  • Thanks Sorin. Its working fine now . sh-4.3$ #! /bin/bash sh-4.3$ currentdate=$(date -u +%s -d '2007-09-01 17:30:24') sh-4.3$ targetdate=$(date -u +%s -d '2007-12-25 12:30:00') sh-4.3$ minutes=$(( ($targetdate - $currentdate) / 60 )) sh-4.3$ echo $minutes 165299 Thanks a lot to all –  Nov 08 '16 at 11:32