linux: date 命令的用法
一、date 命令的用法
1、zsh(ubuntu22.04.2)
1 ➜ null date "+%Y-%m-%d %H:%M"
2 2023-09-10 03:10
3 ➜ null
4 ➜ null
5 ➜ null date "+%F %T"
6 2023-09-10 03:10:13
7 ➜ null
8 ➜ null
9 ➜ null date --help
10 Usage: date [OPTION]... [+FORMAT]
11 or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
12 Display the current time in the given FORMAT, or set the system date.
13
14 Mandatory arguments to long options are mandatory for short options too.
15 -d, --date=STRING display time described by STRING, not 'now'
16 --debug annotate the parsed date,
17 and warn about questionable usage to stderr
18 -f, --file=DATEFILE like --date; once for each line of DATEFILE
19 -I[FMT], --iso-8601[=FMT] output date/time in ISO 8601 format.
20 FMT='date' for date only (the default),
21 'hours', 'minutes', 'seconds', or 'ns'
22 for date and time to the indicated precision.
23 Example: 2006-08-14T02:34:56-06:00
24 -R, --rfc-email output date and time in RFC 5322 format.
25 Example: Mon, 14 Aug 2006 02:34:56 -0600
26 --rfc-3339=FMT output date/time in RFC 3339 format.
27 FMT='date', 'seconds', or 'ns'
28 for date and time to the indicated precision.
29 Example: 2006-08-14 02:34:56-06:00
30 -r, --reference=FILE display the last modification time of FILE
31 -s, --set=STRING set time described by STRING
32 -u, --utc, --universal print or set Coordinated Universal Time (UTC)
33 --help display this help and exit
34 --version output version information and exit
35
36 FORMAT controls the output. Interpreted sequences are:
37
38 %% a literal %
39 %a locale's abbreviated weekday name (e.g., Sun)
40 %A locale's full weekday name (e.g., Sunday)
41 %b locale's abbreviated month name (e.g., Jan)
42 %B locale's full month name (e.g., January)
43 %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
44 %C century; like %Y, except omit last two digits (e.g., 20)
45 %d day of month (e.g., 01)
46 %D date; same as %m/%d/%y
47 %e day of month, space padded; same as %_d
48 %F full date; like %+4Y-%m-%d
49 %g last two digits of year of ISO week number (see %G)
50 %G year of ISO week number (see %V); normally useful only with %V
51 %h same as %b
52 %H hour (00..23)
53 %I hour (01..12)
54 %j day of year (001..366)
55 %k hour, space padded ( 0..23); same as %_H
56 %l hour, space padded ( 1..12); same as %_I
57 %m month (01..12)
58 %M minute (00..59)
59 %n a newline
60 %N nanoseconds (000000000..999999999)
61 %p locale's equivalent of either AM or PM; blank if not known
62 %P like %p, but lower case
63 %q quarter of year (1..4)
64 %r locale's 12-hour clock time (e.g., 11:11:04 PM)
65 %R 24-hour hour and minute; same as %H:%M
66 %s seconds since 1970-01-01 00:00:00 UTC
67 %S second (00..60)
68 %t a tab
69 %T time; same as %H:%M:%S
70 %u day of week (1..7); 1 is Monday
71 %U week number of year, with Sunday as first day of week (00..53)
72 %V ISO week number, with Monday as first day of week (01..53)
73 %w day of week (0..6); 0 is Sunday
74 %W week number of year, with Monday as first day of week (00..53)
75 %x locale's date representation (e.g., 12/31/99)
76 %X locale's time representation (e.g., 23:13:48)
77 %y last two digits of year (00..99)
78 %Y year
79 %z +hhmm numeric time zone (e.g., -0400)
80 %:z +hh:mm numeric time zone (e.g., -04:00)
81 %::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
82 %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
83 %Z alphabetic time zone abbreviation (e.g., EDT)
84
85 By default, date pads numeric fields with zeroes.
86 The following optional flags may follow '%':
87
88 - (hyphen) do not pad the field
89 _ (underscore) pad with spaces
90 0 (zero) pad with zeros
91 + pad with zeros, and put '+' before future years with >4 digits
92 ^ use upper case if possible
93 # use opposite case if possible
94
95 After any flags comes an optional field width, as a decimal number;
96 then an optional modifier, which is either
97 E to use the locale's alternate representations if available, or
98 O to use the locale's alternate numeric symbols if available.
99
100 Examples:
101 Convert seconds since the epoch (1970-01-01 UTC) to a date
102 $ date --date='@2147483647'
103
104 Show the time on the west coast of the US (use tzselect(1) to find TZ)
105 $ TZ='America/Los_Angeles' date
106
107 Show the local time for 9AM next Friday on the west coast of the US
108 $ date --date='TZ="America/Los_Angeles" 09:00 next Fri'
109
110 GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
111 Full documentation <https://www.gnu.org/software/coreutils/date>
112 or available locally via: info '(coreutils) date invocation'
113 ➜ null
2、bash(fedora38)
1 [wit@fedora null]$ date "+%Y-%m-%d"
2 2023-09-10
3 [wit@fedora null]$
4 [wit@fedora null]$
5 [wit@fedora null]$ date --help
6 Usage: date [OPTION]... [+FORMAT]
7 or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
8 Display date and time in the given FORMAT.
9 With -s, or with [MMDDhhmm[[CC]YY][.ss]], set the date and time.
10
11 Mandatory arguments to long options are mandatory for short options too.
12 -d, --date=STRING display time described by STRING, not 'now'
13 --debug annotate the parsed date,
14 and warn about questionable usage to stderr
15 -f, --file=DATEFILE like --date; once for each line of DATEFILE
16 -I[FMT], --iso-8601[=FMT] output date/time in ISO 8601 format.
17 FMT='date' for date only (the default),
18 'hours', 'minutes', 'seconds', or 'ns'
19 for date and time to the indicated precision.
20 Example: 2006-08-14T02:34:56-06:00
21 --resolution output the available resolution of timestamps
22 Example: 0.000000001
23 -R, --rfc-email output date and time in RFC 5322 format.
24 Example: Mon, 14 Aug 2006 02:34:56 -0600
25 --rfc-3339=FMT output date/time in RFC 3339 format.
26 FMT='date', 'seconds', or 'ns'
27 for date and time to the indicated precision.
28 Example: 2006-08-14 02:34:56-06:00
29 -r, --reference=FILE display the last modification time of FILE
30 -s, --set=STRING set time described by STRING
31 -u, --utc, --universal print or set Coordinated Universal Time (UTC)
32 --help display this help and exit
33 --version output version information and exit
34
35 FORMAT controls the output. Interpreted sequences are:
36
37 %% a literal %
38 %a locale's abbreviated weekday name (e.g., Sun)
39 %A locale's full weekday name (e.g., Sunday)
40 %b locale's abbreviated month name (e.g., Jan)
41 %B locale's full month name (e.g., January)
42 %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
43 %C century; like %Y, except omit last two digits (e.g., 20)
44 %d day of month (e.g., 01)
45 %D date; same as %m/%d/%y
46 %e day of month, space padded; same as %_d
47 %F full date; like %+4Y-%m-%d
48 %g last two digits of year of ISO week number (see %G)
49 %G year of ISO week number (see %V); normally useful only with %V
50 %h same as %b
51 %H hour (00..23)
52 %I hour (01..12)
53 %j day of year (001..366)
54 %k hour, space padded ( 0..23); same as %_H
55 %l hour, space padded ( 1..12); same as %_I
56 %m month (01..12)
57 %M minute (00..59)
58 %n a newline
59 %N nanoseconds (000000000..999999999)
60 %p locale's equivalent of either AM or PM; blank if not known
61 %P like %p, but lower case
62 %q quarter of year (1..4)
63 %r locale's 12-hour clock time (e.g., 11:11:04 PM)
64 %R 24-hour hour and minute; same as %H:%M
65 %s seconds since the Epoch (1970-01-01 00:00 UTC)
66 %S second (00..60)
67 %t a tab
68 %T time; same as %H:%M:%S
69 %u day of week (1..7); 1 is Monday
70 %U week number of year, with Sunday as first day of week (00..53)
71 %V ISO week number, with Monday as first day of week (01..53)
72 %w day of week (0..6); 0 is Sunday
73 %W week number of year, with Monday as first day of week (00..53)
74 %x locale's date representation (e.g., 12/31/99)
75 %X locale's time representation (e.g., 23:13:48)
76 %y last two digits of year (00..99)
77 %Y year
78 %z +hhmm numeric time zone (e.g., -0400)
79 %:z +hh:mm numeric time zone (e.g., -04:00)
80 %::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
81 %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
82 %Z alphabetic time zone abbreviation (e.g., EDT)
83
84 By default, date pads numeric fields with zeroes.
85 The following optional flags may follow '%':
86
87 - (hyphen) do not pad the field
88 _ (underscore) pad with spaces
89 0 (zero) pad with zeros
90 + pad with zeros, and put '+' before future years with >4 digits
91 ^ use upper case if possible
92 # use opposite case if possible
93
94 After any flags comes an optional field width, as a decimal number;
95 then an optional modifier, which is either
96 E to use the locale's alternate representations if available, or
97 O to use the locale's alternate numeric symbols if available.
98
99 Examples:
100 Convert seconds since the Epoch (1970-01-01 UTC) to a date
101 $ date --date='@2147483647'
102
103 Show the time on the west coast of the US (use tzselect(1) to find TZ)
104 $ TZ='America/Los_Angeles' date
105
106 Show the local time for 9AM next Friday on the west coast of the US
107 $ date --date='TZ="America/Los_Angeles" 09:00 next Fri'
108
109 GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
110 Full documentation <https://www.gnu.org/software/coreutils/date>
111 or available locally via: info '(coreutils) date invocation'
112 [wit@fedora null]$
113 [wit@fedora null]$
二、参考资料:
1、[wit@fedora null]$ date --help
2、➜ null date --help
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/17690620.html