#! /bin/bash beginTime=$(date "+%Y-%m-%d %H:%M:%S") echo "beginTime:" $beginTime sleep 12 endTime=$(date "+%Y-%m-%d %H:%M:%S") echo "endtime:" $endTime duration=$(($(date +%s -d "${endTime}")-$(date +%s -d "${beginTime}"))); echo "时间差:" $duration"s"
其中
%s表示seconds since 1970-01-01 00:00:00 UTC -d表示display time described by STRING, not 'now'
执行演示:
# source a.sh beginTime: 2022-01-20 19:19:30 endtime: 2022-01-20 19:19:42 时间差: 12s