回到手册索引

命令用途

date 是 Linux/Unix 系统中的一个命令,用于显示当前的系统日期和时间,或根据指定的格式输出日期和时间信息。它也可以用于设置系统日期和时间。

常用用法示例

  1. 显示当前日期和时间

    1
    2
    date
    Thu Feb 22 14:35:20 CST 2025

    显示当前的日期和时间,格式为 星期 月 日 时:分:秒 时区 年。

  2. 显示指定格式的日期和时间

    1
    2
    date "+%Y-%m-%d %H:%M:%S"
    2024-02-22 14:35:20

    使用 + 后跟格式化字符串来指定输出格式,这里是 年-月-日 时:分:秒。

  3. 显示当前日期的星期几

    1
    2
    date "+%A"
    Thursday

    %A 显示完整的星期几名称。

  4. 显示当前月份的英文缩写

    1
    2
    date "+%b"
    Feb

    %b 显示当前月份的英文缩写。

  5. 显示自定义格式的日期(仅显示年和月)

    1
    2
    date "+%Y-%m"
    2024-02

    %Y 表示年份,%m 表示月份。

  6. 显示 Unix 时间戳(自1970年1月1日起的秒数)

    1
    2
    date "+%s"
    1677051320

    %s 显示自 1970 年 1 月 1 日以来的秒数(Unix 时间戳)。

  7. 将指定日期设置为当前日期(需要超级用户权限)

    1
    2
    sudo date 022214352025
    Thu Feb 22 14:35:00 CST 2025

    将系统时间设置为 2025 年 2 月 22 日 14:35:00。命令格式为 MMDDhhmm[[CC]YY][.ss]。

  8. 指定输出日期为 UTC 时间

    1
    2
    date -u
    Thu Feb 22 06:35:20 UTC 2025

    使用 -u 选项来显示 UTC 时间,而非本地时区时间。

常用参数选项

  • +FORMAT
    用于指定自定义输出格式,FORMAT 是一个由占位符组成的字符串。常用的占位符包括 %Y(年份)、%m(月份)、%d(日期)、%H(小时)、%M(分钟)等。

  • -u
    显示 UTC 时间,而不是本地时区时间。

  • -s
    用于设置系统时间,格式为 YYYY-MM-DD HH:MM:SS,如 date -s “2024-02-22 14:35:00”。

  • -d 或 –date
    用于指定一个时间字符串,返回对应的时间。例如:date -d “next Fri”。

  • -R 或 –rfc-2822
    显示符合 RFC 2822 标准的日期格式,例如:Thu, 22 Feb 2025 14:35:20 +0800。

  • -I 或 –iso-8601
    显示符合 ISO 8601 标准的日期格式,如 2024-02-22T14:35:20+08:00。可以通过不同的参数进一步指定格式。

  • -v
    用于显示相对于当前时间的日期。例如:date -v+1d 显示明天的日期,date -v-1d 显示昨天的日期。

  • -f FILE
    从文件中读取时间字符串并转换为相应的日期。文件中的每一行都应该是有效的时间字符串。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
NAME 

date - print or set the system date and time

SYNOPSIS

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

DESCRIPTION

Display date and time in the given FORMAT. With -s, or with
[MMDDhhmm[[CC]YY][.ss]], set the date and time.

Mandatory arguments to long options are mandatory for short
options too.

-d, --date=STRING
display time described by STRING, not 'now'

--debug
annotate the parsed date, and warn about questionable usage
to stderr

-f, --file=DATEFILE
like --date; once for each line of DATEFILE

-I[FMT], --iso-8601[=FMT]
output date/time in ISO 8601 format. FMT='date' for date
only (the default), 'hours', 'minutes', 'seconds', or 'ns'
for date and time to the indicated precision. Example:
2006-08-14T02:34:56-06:00

--resolution
output the available resolution of timestamps Example:
0.000000001

-R, --rfc-email
output date and time in RFC 5322 format. Example: Mon, 14
Aug 2006 02:34:56 -0600

--rfc-3339=FMT
output date/time in RFC 3339 format. FMT='date',
'seconds', or 'ns' for date and time to the indicated
precision. Example: 2006-08-14 02:34:56-06:00

-r, --reference=FILE
display the last modification time of FILE

-s, --set=STRING
set time described by STRING

-u, --utc, --universal
print or set Coordinated Universal Time (UTC)

--help display this help and exit

--version
output version information and exit

All options that specify the date to display are mutually
exclusive. I.e.: --date, --file, --reference, --resolution.

FORMAT controls the output. Interpreted sequences are:

%% a literal %

%a locale's abbreviated weekday name (e.g., Sun)

%A locale's full weekday name (e.g., Sunday)

%b locale's abbreviated month name (e.g., Jan)

%B locale's full month name (e.g., January)

%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)

%C century; like %Y, except omit last two digits (e.g., 20)

%d day of month (e.g., 01)

%D date (ambiguous); same as %m/%d/%y

%e day of month, space padded; same as %_d

%F full date; like %+4Y-%m-%d

%g last two digits of year of ISO week number (ambiguous;
00-99); see %G

%G year of ISO week number; normally useful only with %V

%h same as %b

%H hour (00..23)

%I hour (01..12)

%j day of year (001..366)

%k hour, space padded ( 0..23); same as %_H

%l hour, space padded ( 1..12); same as %_I

%m month (01..12)

%M minute (00..59)

%n a newline

%N nanoseconds (000000000..999999999)

%p locale's equivalent of either AM or PM; blank if not known

%P like %p, but lower case

%q quarter of year (1..4)

%r locale's 12-hour clock time (e.g., 11:11:04 PM)

%R 24-hour hour and minute; same as %H:%M

%s seconds since the Epoch (1970-01-01 00:00 UTC)

%S second (00..60)

%t a tab

%T time; same as %H:%M:%S

%u day of week (1..7); 1 is Monday

%U week number of year, with Sunday as first day of week
(00..53)

%V ISO week number, with Monday as first day of week (01..53)

%w day of week (0..6); 0 is Sunday

%W week number of year, with Monday as first day of week
(00..53)

%x locale's date (can be ambiguous; e.g., 12/31/99)

%X locale's time representation (e.g., 23:13:48)

%y last two digits of year (ambiguous; 00..99)

%Y year

%z +hhmm numeric time zone (e.g., -0400)

%:z +hh:mm numeric time zone (e.g., -04:00)

%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)

%:::z numeric time zone with : to necessary precision (e.g., -04,
+05:30)

%Z alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':

- (hyphen) do not pad the field

_ (underscore) pad with spaces

0 (zero) pad with zeros

+ pad with zeros, and put '+' before future years with >4
digits

^ use upper case if possible

# use opposite case if possible

After any flags comes an optional field width, as a decimal
number; then an optional modifier, which is either E to use the
locale's alternate representations if available, or O to use the
locale's alternate numeric symbols if available.

EXAMPLES

Convert seconds since the Epoch (1970-01-01 UTC) to a date

$ date --date='@2147483647'

Show the time on the west coast of the US (use tzselect(1) to find
TZ)

$ TZ='America/Los_Angeles' date

Show the local time for 9AM next Friday on the west coast of the
US

$ date --date='TZ="America/Los_Angeles" 09:00 next Fri'

DATE STRING

The --date=STRING is a mostly free format human readable date
string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29
16:21:42" or even "next Thursday". A date string may contain
items indicating calendar date, time of day, time zone, day of
week, relative time, relative date, and numbers. An empty string
indicates the beginning of the day. The date string format is
more complex than is easily documented here but is fully described
in the info documentation.

AUTHOR

Written by David MacKenzie.

REPORTING BUGS

GNU coreutils online help:
<https://www.gnu.org/software/coreutils/>
Report any translation bugs to
<https://translationproject.org/team/>

COPYRIGHT

Copyright © 2025 Free Software Foundation, Inc. License GPLv3+:
GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

Full documentation <https://www.gnu.org/software/coreutils/date>
or available locally via: info '(coreutils) date invocation'

COLOPHON

This page is part of the coreutils (basic file, shell and text
manipulation utilities) project. Information about the project
can be found at ⟨http://www.gnu.org/software/coreutils/⟩. If you
have a bug report for this manual page, see
⟨http://www.gnu.org/software/coreutils/⟩. This page was obtained
from the tarball coreutils-9.6.tar.xz fetched from
⟨http://ftp.gnu.org/gnu/coreutils/⟩ on 2024-02-02. If you
discover any rendering problems in this HTML version of the page,
or you believe there is a better or more up-to-date source for the
page, or you have corrections or improvements to the information
in this COLOPHON (which is not part of the original manual page),
send a mail to man-pages@man7.org