顯示設定系統時間指令date,在寫shell腳本時經常會用到,date指令參數比較多,記住幾個常用的即可!
-d 字元串:顯示字元串所指的日期與時間,字元串前後必須加上雙引号
-s 字元串:根據字元串來設定日期與時間,字元串前後必須加上雙引号
-u:顯示或設定通用時間時間域,utc,cst,gmt
--help:幫助
--version:版本資訊
%% a literal % 顯示字元%
%a locale's abbreviated weekday name (e.g., sun) 星期的簡稱
%a locale's full weekday name (e.g., sunday) 星期的完整名稱
%b locale's abbreviated month name (e.g., jan) 月的簡稱
%b locale's full month name (e.g., january) 月的完整名稱
%c locale's date and time (e.g., thu mar 3 23:05:25 2005) 顯示年月日時間(例mon 21 nov 2016 11:15:40 am cst )
%c century; like %y, except omit last two digits (e.g., 20) 世紀
%d day of month (e.g, 01) 日期(01-31)
%d date; same as %m/%d/%y 年月日
%e day of month, space padded; same as %_d 日期(1-31)
%f full date; same as %y-%m-%d 日期(和%y-%m-%d一樣)
%g last two digits of year of iso week number (see %g) 隻顯示年的後兩位16
%g year of iso week number (see %v); normally useful only with %v 完整年份2016
%h same as %b 和%b一樣
%h hour (00..23) 24小時制(00-23)
%i hour (01..12) 12小時制(01-12)
%j day of year (001..366) 一年第幾天(001-366)
%k hour ( 0..23) 24小時制(0-23)
%l hour ( 1..12) 12小時制(1-12)
%m month (01..12) 月份(01-12)
%m minute (00..59) 分鐘(00-59)
%n a newline 換行
%n nanoseconds (000000000..999999999) 納秒(不明白的可以去搜納秒)
%p locale's equivalent of either am or pm; blank if not known am或pm
%p like %p, but lower case am或pm
%r locale's 12-hour clock time (e.g., 11:11:04 pm) 顯示時間12小時制
%r 24-hour hour and minute; same as %h:%m 顯示時間24小時制
%s seconds since 1970-01-01 00:00:00 utc 從1970年1月1日00:00:00到目前所經曆的秒數
%s second (00..60) 秒(00-60)
%t a tab 制表符
%t time; same as %h:%m:%s 24小時制時間
%u day of week (1..7); 1 is monday 一周的第幾天,1就是星期一
%u week number of year, with sunday as first day of week (00..53) 一年的第幾周,周日為每周的第一天(00-53)
%v iso week number, with monday as first day of week (01..53) 一年的第幾周,周一為每周的第一天(01-53)
%w day of week (0..6); 0 is sunday 一周的第幾天(0-6),0表示周日
%w week number of year, with monday as first day of week (00..53) 一年的第幾周,周一為每周的第一天(00-53)
%x locale's date representation (e.g., 12/31/99) 日期(mm/dd/yy)
%x locale's time representation (e.g., 23:13:48) 時間(%h:%m:%s)
%y last two digits of year (00..99) 年份(00-99)和%g一樣
%y year 見%g
%z alphabetic time zone abbreviation (e.g., edt) 時區
設定時間
date -s 11:28:23 設定時間為11點28分23秒,原來的年月日不變
設定所有時間
date -s "11:28:23 2016-10-10" 這樣年月日也随之改變
設定年月
date -s 20161010 這樣具體時間就變成了0點0分0秒
date -s "20120523 02:00:01"
七天前日期
date -d "7 day ago" +"%y-%m-%d"
七天後日期
date -d "-7 day age" +"%y-%m-%d"
上一月日期
date -d "-1 month" +"%y-%m-%d"
下一月日期
date -d "1 month" +"%y-%m-%d"
前一年日期
date -d "-1 year" +"%y-%m-%d"
下一年日期
date -d "1 year" +"%y-%m-%d"