1. 程式人生 > >Linux的date用法

Linux的date用法

con 命令參數 定時 顯示文件 shell腳本 輸出 風格 file option

顯示時間是個常用的命令,在寫shell腳本中也經常會用到與日期相關文件名或時間顯示。無論是linux還是windows下都是date命令。

Linux下date命令用法

  • date [OPTION]… [+FORMAT]
  • date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

date命令參數

  • -d, –date=STRING 顯示STRING指定的時間
  • -f, –file=DATEFILE 類似–date參數顯示DATEFILE文件中的每行時間
  • -ITIMESPEC, –iso-8601[=TIMESPEC] 以ISO 8601 格式顯示日期/時間。TIMESPEC為”date”(只顯示日期)、”hours”、”minutes”、”senconds”(顯示時間精度)之一,默認為”date”。
  • -r, –reference=FILE 顯示文件的最後修改時間
  • -R, –rfc-2822 以RFC-2822兼容日期格式顯示時間
  • -s, –set=STRING 設置時間為STRING
  • -u, –utc, –universal 顯示或設定為Coordinated Universal Time時間格式

date命令輸出顯示格式

  • %% 字符%
  • %a 星期的縮寫(Sun..Sat)
  • %A 星期的完整名稱 (Sunday..Saturday)
  • %b 月份的縮寫(Jan..Dec)
  • %B 月份的完整名稱(January..December)
  • %c 日期時間(Sat Nov 04 12:02:33 EST 1989)
  • %C 世紀(年份除100後去整) [00-99]
  • %d 一個月的第幾天(01..31)
  • %D 日期(mm/dd/yy)
  • %e 一個月的第幾天 ( 1..31)
  • %F 日期,同%Y-%m-%d
  • %g 年份(yy)
  • %G 年份(yyyy)
  • %h 同%b
  • %H 小時(00..23)
  • %I 小時(01..12)
  • %j 一年的第幾天(001..366)
  • %k 小時( 0..23)
  • %l 小時( 1..12)
  • %m 月份(01..12)
  • %M 分鐘(00..59)
  • %n 換行
  • %N 納秒(000000000..999999999)
  • %p AM or PM
  • %P am or pm
  • %r 12小時制時間(hh:mm:ss [AP]M)
  • %R 24小時制時間(hh:mm)
  • %s 從00:00:00 1970-01-01 UTC開始的秒數
  • %S 秒(00..60)
  • %t 制表符
  • %T 24小時制時間(hh:mm:ss)
  • %u 一周的第幾天(1..7); 1 表示星期一
  • %U 一年的第幾周,周日為每周的第一天(00..53)
  • %V 一年的第幾周,周一為每周的第一天 (01..53)
  • %w 一周的第幾天 (0..6); 0 代表周日
  • %W 一年的第幾周,周一為每周的第一天(00..53)
  • %x 日期(mm/dd/yy)
  • %X 時間(%H:%M:%S)
  • %y 年份(00..99)
  • %Y 年份 (1970…)
  • %z RFC-2822 風格數字格式時區(-0500)
  • %Z 時區(e.g., EDT), 無法確定時區則為空

自定義時間舉例說明:

[[email protected] test]# date
2017年 10月 12日 星期四 03:40:41 CST
[[email protected] test]# date -I
2017-10-12
[ro[email protected] test]# date -Ihours
2017-10-12T03+0800
[[email protected] test]# date -Iminutes
2017-10-12T03:41+0800
[[email protected] test]# date -Iseconds
2017-10-12T03:41:41+0800
[[email protected] test]# date -R
Thu, 12 Oct 2017 03:42:12 +0800
[[email protected] test]# date -u
2017年 10月 11日 星期三 19:42:19 UTC
[[email protected] test]# date +"Today is %A."
Today is 星期四.
[[email protected] test]# date +"Date:%b. %e, %G"
Date:10月. 12, 2017
[[email protected] test]# date +"Date: %b.%e, %G"
Date: 10月.12, 2017
[[email protected] test]# date +"%x %X"
2017年10月12日 03時42分56秒
[[email protected] test]# date +"%Y-%m-%d %H:%M:%S"
2017-10-12 03:43:07
[[email protected] test]# date +"%Y-%m-%d %I:%M:%S %p"
2017-10-12 03:43:27 上午
[[email protected] test]# 

常用時間格式:

[[email protected] test]# date "+%Y-%m-%d %H:%M:%S"
2017-10-12 03:45:36
[[email protected] test]# date "+%Y-%m-%d"
2017-10-12
[[email protected] test]#

Linux date 日期加減運算

[[email protected] test]# date    #正常時間
2017年 10月 12日 星期四 03:49:52 CST
[[email protected] test]# date +"%b %e, %G"    #自定義時間格式
10月 12, 2017
[[email protected] test]# date +"%b %e, %G" -d-1 day    #時間減1天,加1天類似
10月 11, 2017
[[email protected] test]# date +"%b %e, %G" -d+1 month    #時間加一個月,減一個月類似
11月 12, 2017
[[email protected] test]# date +"%Y年%m月%d日"    #自定義時間格式,年月日
2017年10月12日
[[email protected] test]# date +"%Y年%m月%d日" -d-1 day    #同上面加減1天(或加減1月)
2017年10月11日
[[email protected] test]# date +"%Y年%-m月%d日"    #去除月份前的0,例如‘2017年08月12日’顯示為‘2017年8月12日’
2017年10月12日
[[email protected] test]# date -d-1 day +"%b %e, %G"     #時間減1天,加1天類似
10月 11, 2017
[[email protected] test]# date -d+1 month +"%b %e, %G"     #時間加一個月,減一個月類似
11月 12, 2017
[[email protected] test]# 

Linux的date用法