我有两个约会, 例如 2016年2月15日星期一00:03:25 GMT 2月16日星期二00:00:15 GMT
我需要计算ksh unix中小时/分钟的差异。
如何做到这一点,有人可以帮助
答案 0 :(得分:0)
使用GNU日期(来自GNU Core Utilities):
# convert dates to UNIX timestamps (seconds since 00:00 Jan 1 1970)
date1=$( date -d "Mon Feb 15 00:03:25 GMT 2016" +"%s" )
date2=$( date -d "Tue Feb 16 00:00:15 GMT 2016" +"%s" )
diff_seconds=$(( date2 - date1 )) # total seconds
diff_hours=$(( diff_seconds/60/60 )) # whole hours
# deduct whole hours from total seconds
diff_seconds=$(( diff_seconds - 60*60*diff_hours ))
diff_minutes=$(( diff_seconds/60 )) # remaining whole minutes
# deduct whole minutes from remaining seconds
diff_seconds=$(( diff_seconds - 60*diff_minutes ))
printf "%d hours, %d minutes, %d seconds\n" \
diff_hours diff_minutes diff_seconds