Skip to main content

date

Shell 範例
# Sample
NOW=`date "+%Y/%m/%d %H:%M:%S"`

eval `date "+day=%d; month=%m; year=%Y"`
BKNAME="cacti-backup-$year-$month-$day.tar.gz"

# Sample
# %T time; same as %H:%M:%S
NOWD=$(date +"F")   # YYYY-MM-DD
NOWT=$(date +"%T")  # H:M:S
將 Date 轉換為 EPOCH 時間格式
# 用 perl 可用於 AIX
perl -e 'use Time::Local; print timelocal(0,25,1,11,11,2008), "\n";'
常用格式
# echo `date "+%y-%m-%d_%H%M%S"`
17-05-22_105503

# echo `date "+%Y/%m/%d"`
2015/07/17

# echo `date "+%F"`
2015-07-17

# Display past date
date --date="49 days ago"
日  9月 11 16:17:02 CST 2016

date --date="1 month ago"
五  9月 30 16:17:34 CST 2016

# Display future date
date --date="next fri"
五  2月  5 00:00:00 CST 2021

date --date='TZ="America/New_York" 10:00 next fri'
五  2月  5 23:00:00 CST 2021
計算程式執行所花費的時間
start=`date "+%Y/%m/%d %H:%M:%S"`
start_s=$(date -d "$start" +%s)

Sleep 20

end=`date "+%Y/%m/%d %H:%M:%S"`
end_s=$(date -d "$end" +%s)
diff=$((end_s - start_s))