Use of Linux date Command

  • 2021-08-28 21:44:03
  • OfStack

1. Brief introduction to commands

The date command is used to display the current time or the specified time in the specified format, or to set the system time. Many Shell scripts need to print time or date in different formats and perform actions according to time and date, which can be done by using date command. In an Unix-like system, the date is stored as an integer whose size is the number of seconds elapsed from 0:00:0 on January 1, 1970, Coordinated Universal Time (UTC), that is, the Unix timestamp.

2. Command format


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

Where FORMAT is a format control string and can be taken as follows:


%% 字符%
%a 星期的简称(Sun~Sat)
%A 星期的全称(Sunday~Saturday)
%b 月的简称(Jan~Dec)
%B 月的全称(January~December)
%c 日期和时间(Thu 06 Dec 2018 09:43:53 AM CST)。只输入date指令也会显示同样的结果。
%C 世纪。和%Y比较像,但不显示最后两个数字,如20
%d 1个月的第几天(01~31)
%D 日期,等同于%m/%d/%y,如12/06/18
%e 1个月的第几天(1~31),单数字以空格填充,等同于%_d
%F 日期,等同于%Y-%m-%d,如2018-12-06
%g 年的最后两个数字(yy),比如2018则输出18,等同于%y
%G 年份(yyyy)
%h 月的简称(Jan~Dec),等同于%b
%H 小时,24小时制(00~23)
%I 小时,12小时制(01~12)
%j 1年的第几天(001~366)
%k 小时,24小时制(0~23)。单数字填充空格,等同于%_H
%l 小时,12小时制(1~12)。单数字填充空格,等同于%_I
%m 月份(01~12)
%M 分钟(00~59)
%n 换行符newline
%N 纳秒nanoseconds(000000000..999999999)
%p 显示出AM或PM
%P 显示出am或pm
%r 显示时间,12小时制(hh:mm:ss %p)
%R 显示小时与分钟,24小时制,等同于%H:%M
%s 从1970年1月1日00:00:00到目前经历的秒数
%S 显示秒(00~59)
%t Tab符
%T 显示时间,24小时制(hh:mm:ss),等同于%H:%M:%S
%u 1周的第几天(1..7)。1表示星期1
%U 1年的第几周,周日为每周的第1天(00..53)
%V 1年的第几周,周1为每周的第1天(01..53)
%w 1个星期的第几天(0~6),0代表星期天
%W 1年的第几周,周1为每周的第1天(00..53)
%x 日期(mm/dd/yyyy),如12/06/2018
%X 时间,等同于%H:%M:%S
%y 年的最后两个数字(2018则是18)
%Y 年(yyyy)
%z 以+hhmm格式显示时区(如+0800)
%:z 以+hh:mm格式显示时区(如+08:00)
%::z 以+hh:mm:ss格式显示时区(如+08:00:00)
%Z 缩写显示时区名称,如CST(China Standard Time)
%h,%b 月的简称(Jan~Dec)
填充字符说明:默认地,date命令以0填充数字域,以下填充字符的控制符可以跟在%后使用:
- (hyphen,连字符):不进行填充
_ (underscore,下划线):以空格填充
0(zero)以0填充
^ 尽可能地使用大写输出
# 尽可能地按照相反的大小写进行输出

3. Command options


-d, --date=STRING Displays the value of the STRING The specified time instead of the current timestamp; 
-f, --file=DATEFILE : Display DATEFILE Time per line in the file; 
-I[TIMESPEC], --iso-8601[=TIMESPEC] : To ISO 8601 The specification format follows the specified precision [TIMESPEC] Displays the time. TIMESPEC The default value is "date" , also desirable value 'hours', 'minutes', 'seconds',  Or  'ns' ; 
-r, --reference=FILE Displays when the file was last modified 
-R, --rfc-2822 : To RFC-2822 Specify the format display time, such as: Wed, 05 Dec 2018 22:10:34 +0800
--rfc-3339=TIMESPEC : To RFC 3339 Display time in a specified format, which can be displayed by TIMESPEC Indicate precision, TIMESPEC Valuable value 'date', 'seconds', or 'ns' . For example: 2018-12-05 22:09:59.230994842+08:00
-s, --set=STRING Set system time to STRING The specified time 
-u, --utc, --universal Displays or sets to Coordinated Universal Time ( UTC , Coordinated Universal Time ) Time format 
--help : Display date Help information for commands 
--version : Display date Version information of the command 

4. Common examples

(1) Get the Unix timestamp.


date +%s
1544067345

(2) Convert the Unix timestamp to a readable time.


date -d @1483525407
Wed Jan 4 18:23:27 CST 2017

date -d @1483525407 +"%F %T"
2017-01-04 18:23:27

Note:-d should be followed by a date in legal format, so the timestamp needs to be distinguished by adding @ symbol.

(3) Format the output current time.


date +"%Y-%m-%d %H:%M:%S"
2018-12-06 10:57:33

# Or 
date +"%F %T"

(4) Time addition and subtraction operation.


date +"%Y-%m-%d %H:%M:%S"   				// Show the current time 
date -d "+1 day" +"%Y-%m-%d %H:%M:%S" 		// Before Display 1 The time of days 
date -d "-1 day" +"%Y-%m-%d %H:%M:%S" 		// After display 1 The time of days 
date -d "-1 month" +"%Y-%m-%d %H:%M:%S"   // On display 1 The time of the month 
date -d "+1 month" +"%Y-%m-%d %H:%M:%S"   // Display under 1 The time of the month 
date -d "-1 year" +"%Y-%m-%d %H:%M:%S"   // Before Display 1 The time of the year 
date -d "+1 year" +"%Y-%m-%d %H:%M:%S"   // Display under 1 The time of the year 

(5) Conversion of common format.


date -d "2009-12-12" +"%Y/%m/%d %H:%M:%S"
2009/12/12 00:00:00

(6) Set the system time.


date -s "2016-11-11 00:00:00"
Fri Nov 11 00:00:00 CST 2016

date
Fri Nov 11 00:00:05 CST 2016

The above is the use of Linux date command details, more about Linux date command information please pay attention to other related articles on this site!


Related articles: