Bash script: difference in minutes between two times

前端 未结 7 1631
不思量自难忘°
不思量自难忘° 2020-12-31 07:55

I have two time strings; eg. \"09:11\" and \"17:22\" on the same day (format is hh:mm). How do I calculate the time difference in minutes between these two?

Can the

7条回答
  •  半阙折子戏
    2020-12-31 08:16

    I would convert the dates to UNIX timestamps; you can subtract to get the difference in seconds, then divide by 60:

    #!/bin/bash
    
    MPHR=60    # Minutes per hour.
    
    CURRENT=$(date +%s -d '2007-09-01 17:30:24')
    TARGET=$(date +%s -d'2007-12-25 12:30:00')
    
    MINUTES=$(( ($TARGET - $CURRENT) / $MPHR ))
    

提交回复
热议问题