Preface: This article is the content of the basic Linux operation chapter!
The environment I use is based on Tencent Cloud Server: CentOS 7.6 64bit.
Study set:
Directory index:
1. Understanding of basic grammatical forms
2. Obtaining time under Linux
3. Formatted output of the current time
- - 3.1 Format control character description
- - 3.2 Formatted output writing method and examples
4. Related articles or series recommendations
1. Understanding of basic grammatical forms
Form 1: date [Use this command directly]
Form 2: date + [format]: [ie: date + formatted form]
Form three: date [option] [format]
2. Get time under Linux
Directly displayed information format:
- Day of the week, month, day, current time, CST, year
[Mortal@VM-12-16-centos test_txtfile]$ date
Sun May 28 16:27:53 CST 2023
3. Format and output the current time
3.1 Description of format control characters
format control character | Meaning and data range |
---|---|
%H | Hours (00~23) |
%M | Minutes (00~59) |
%S | Seconds (00~61) |
%X | Equivalent to: %H:%M:%S |
%d | Sun (01~31) |
%m | Month (01~12) |
%Y | Complete year (0000~9999) |
%F | Equivalent to: %Y-%m-%d |
3.2 Formatted output writing method and examples
Note: Each placeholder can be spliced without gaps. Insert specified symbols between placeholders to specify the display format! [Example below]
Pay attention to the writing method: date +[format]
Note:
- There must be a space between date and + sign!
- There can be no space between the format control string and the + sign!
- There cannot be spaces between format control string placeholders!
Output form | How to write output instructions | Example |
---|---|---|
(number) year, month and day | date +%Y%m%d | 20230528 |
(Number) Year, Month, Day_Hour:Minute:Second | date +%Y%m%d_%H:%M:%S | 20230528_16:44:58 |
year month day | date +%Y-%m-%d | 2023-05-28 |
year month day | date +%Y*%m*%d | 20230528 |
year month day | date +%Y/%m/%d | 2023/05/28 |
x year x month x day | date +%Y year %m month %d day | June 5, 2023 |
/* 无间隔直接使用:输出年月日 */
[Mortal@VM-12-16-centos test_txtfile]$ date +%Y%m%d
20230528
/* 无间隔直接使用:输出年月日时分秒 */
[Mortal@VM-12-16-centos test_txtfile]$ date +%Y%m%d_%H:%M:%S
20230528_16:44:58
/* 格式化输出:年-月-日 */
[Mortal@VM-12-16-centos test_txtfile]$ date +%Y-%m-%d
2023-05-28
/* 格式化输出:年*月*日 及 年/月/日 */
[Mortal@VM-12-16-centos test_txtfile]$ date +%Y*%m*%d
2023*05*28
[Mortal@VM-12-16-centos test_txtfile]$ date +%Y/%m/%d
2023/05/28
/* 输出格式化:x年x月x日 */
[Mortal@VM-12-16-centos test_file]$ date +%Y年%m月%d日
2023年06月05日