Get the App
SLTechnology News&Howtos  ›  Servers  › 

Detailed explanation of Linux small Command date

Shulou Source: shulou.com Published: 2022-06-02 20:04:44 10月01日 Update

The usage of Linux date command

1. Command function: date can be used to display and set the date and time of the system.

2. Command parameters:

-d: displays the date and time indicated by the string. Double quotation marks before and after the string: set the date and time according to the string. Double quotation marks around the string-u: display GMT--help: online help-- version: display version information

3. List of date format strings

% n: next line% t: tabbed% H: hours (00-23)% I: hours (01-12)% k: hours (0-23)% l: hours (1-12)% M: minutes (00-59)% p: display local AM or PM%r: direct display time (12-hour system) Format: hh:mm:ss [AP] M)% s: number of seconds since 00:00:00 UTC on January 1, 1970% S: seconds (00-60)% T: direct display time (24-hour system)% X: equivalent to% H:%M:%S%Z: display time zone% a: day of the week (Sun-Sat)% A: day of the week (Sunday-Saturday)% b: month (Jan-Dec)% B: month (January-December)% c: direct display date and time% d: day (01-31)% D: direct display date (mm/dd/yy)% h: same% b% j: day of year (001-366)% m: month (01-12)% U: week of year (00-53) (with Sunday as The first day of the week)% w: day of the week (0-6)% W: week of the year (00-53) (in the case of Monday as the first day of the week)% x: direct display date (mm/dd/yy)% y: last two digits of the year (00.99)% Y: full year (0000-9999)

4. Examples

Displays the current time in the format: 2019-09-18 10:20:30 [root@localhost ~] # date "+% F% T"

What day was it the day before yesterday?

[root@localhost ~] # date +% A-- date'- 2day'

Set the current time to: 2019-09-18 10:20:30

[root@localhost] # date-s "2019-09-18 10:20:30"

Output yesterday date

[root@localhost ~] # date-d "- 1day"+% F% T"

Output after 2s

[root@localhost ~] # date-d "2second"+% F% T"

Show the date of the following day

[root@localhost ~] # date-d "1day"+% F% T"

Show the date of last month

[root@localhost ~] # date-d "- 1month"+% F% T"

Show the date of last year

[root@localhost ~] # date-d "- 1year"+% F% T"

Tags: Date time hour character string command week month format year quotation mark situation output line information function parameter online help instance reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB Apple MySQL Linux