[精讚] [會員登入]
7828

Linux shell 的date表示法

linux下SHELL中的date表示法

分享此文連結 //n.sfs.tw/10080

分享連結 Linux shell 的date表示法@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 14:18:51 最後編修
2016-10-27 11:25:27 By 張○○
 

自動目錄

Linux shell中用 csh/tcsh

顯示前一天日期

$ /bin/date --date='1 day ago' +%Y%m%d%H%M%S
20161026214123

顯示前一週日期

$ /bin/date --date='1 week ago' +%Y%m%d%H%M%S
20161020214123

顯示前一月日期

$ /bin/date --date='1 month ago' +%Y%m%d%H%M%S
20160926214123

環境變數 locale

在date 如果叫用 %b的參數會顯示出月份的地區名稱,注意格式前的 '+' 代表的是要輸出的格式
$ date +%a%A%b%B%c%C
結果:
六週六 1月一月西元2016年10月27日 (週四) 15時30分35秒20

如果要用日期作為檔名,我們一般而言不喜歡檔名有中文字。例如檔名是2009Jan,你不希望取出的檔名是"2009一月",則就要把日期輸出改為英文,因此在Linux下先 叫用這一行:
$ export LC_TIME=en_US.utf8
$ date +%a%A%b%B%c%C
SatSaturdayJanJanuarySat 24 Jan 2009 03:31:36 PM CST20

 

有關shell 中date 格式(FORMAT)變數的意義

%  Literal %. <== 代表'%' 符號
-(hyphen)  Do not pad fields (default: pad fields with zeros).  <== 字元不足不用補0,預設有補0
_ (underscore)  Pad fields with space (default: zeros). <== 字元不足用空白補
%a Abbreviated weekday.
%b Abbreviated month name.
%c Country-specific date and time format.
%d Day of month (01-31).
%h Same as %b.
%j Julian day of year (001-366).
%k Hour in 24-hour format, without leading zeros (0-23).
%l Hour in 12-hour format, without leading zeros (1-12).
%m Month of year (01-12).
%n Insert a new line.
%p String to indicate a.m. or p.m.
%r Time in %I:%M:%S %p (12-hour) format.
%s Seconds since "the Epoch," which is 1970-01-01 00:00:00 UTC (a nonstandard extension).
%t Insert a tab.
%w Day of week (Sunday = 0).
%x Country-specific date format based on locale.
%y Last two digits of year (00-99).
%z RFC 822-style numeric time zone.
%A Full weekday.
%B Full month name.
%D Date in %m/%d/%y format.
%H Hour in 24-hour format (00-23).
%I Hour in 12-hour format (01-12).
%M Minutes (00-59).
%S Seconds (00-59).
%T Time in %H:%M:%S format.
%U Week number in year (00-53); start week on Sunday.
%V Week number in year (01-52); start week on Monday.
%W Week number in year (00-53); start week on Monday.
%X  Country-specific time format based on locale.
%Y Four-digit year (e.g., 2006).
%Z Time-zone name.

原文編修 2008-11-24 15:39:12

END

你可能感興趣的文章

[Rocky9] Rocky Linux 9網路重啟 network restart? Rocky Linux 9網路重啟 network restart 指令不見了,該怎麼辦?

[APACHE] PHP<5.5及 PHP>=5.5 安裝APC php5.5版後的apc只能手動安裝,此文包含php<5.5及php>=5.5兩種安裝方法

[Centos7] 新的防火牆firewalld Centos7新的防火牆firewalld 和前面的iptables有很大的不同

[Centos6] 安裝VNC連線程式 在Centos6桌上環境安裝VNC 連線程式

EXCEL+phpPgAdmin 使用phpPgAdmin 匯出資料 直接使用phpPgAmin下載資料的方法

使用dnstop 來觀察dns運作的情形 dnstop是非常方便的工具,可以觀察即時的dns查詢。 安裝 yum install dnstop

隨機好文

為什麼要買長達二十年的保單? 為什麼要買長達二十年的保單?找一個可以說服我買二十年保單的理由。

[HP DL380G7] 生效啟動第3,4片網卡/開啟或關閉內建的網卡 HP DL380G7 預設第3,4片網卡裝完系統後找不到,難道是壞了?要怎麼辦?

[札記] 2016.7~12月札記 札記,只是札記

[bc] linux 的計算機 bc 設定小數位數、計算π、次方根 linux 的計算機 bc 設定小數位數、計算π、次方根

精讚的版面變化 ▓此文僅作為舊文的記錄▓ 這篇文章為了紀念改版完成而撰寫。 原本的部落格是民國97年的作品,那時還是用舊有的技術來寫,很