linux date命令
Unix/Linux date command is a command used for displaying the system’s date and time. In addition, the command can also be used to modify or set the system’s date and time. For one to change the operating system’s time and date, one must be logged in as the root user. In this guide, we examine the Linux date command and how it is used.
Unix / Linux date命令是用于显示系统日期和时间的命令。 另外,该命令还可以用于修改或设置系统的日期和时间。 要更改操作系统的时间和日期,必须以root用户身份登录。 在本指南中,我们检查Linux date命令及其用法。
Linux Date命令无选项 (Linux Date command with no option)
When used as it is without any options, date command displays system’s date and time as shown:
当不带任何选项使用时, date命令将显示系统的日期和时间,如下所示:
$ date
Sample Output
样本输出
The above snippet shows the time in the EAT (East Africa Timezone) timezone (local system timezone).
上面的代码片段显示了EAT(东非时区)时区(本地系统时区)中的时间。
以格林威治标准时间(UTC)显示系统时间 (Display system time in Greenwich Mean-time (UTC))
In our previous example, the system time was display in EAT timezone. To display time in UTC or Greenwich Mean Time, use the -u
option
在我们之前的示例中,系统时间以EAT时区显示。 要以UTC或格林威治标准时间显示时间,请使用-u
选项
$ date -u
Sample Output
样本输出
使用timedatectl命令同时显示本地时间和UTC时间 (Display both local and UTC time using timedatectl command)
Another handy command when it comes to displaying system time is the timedatectl
command. Using the command, you can display both local and UTC time as shown.
关于显示系统时间的另一个方便的命令是timedatectl
命令。 使用该命令,可以显示本地时间和UTC时间,如图所示。
$ timedatectl
Sample Output
样本输出
带–date或-d选项的日期 (Date with –date or -d option)
The -d
or --date
option is used to convert date from numerical to string format. For example, to convert 3/12/2019
to a string, run
-d
或--date
选项用于将日期从数字格式转换为字符串格式。 例如,要将3/12/2019
转换为字符串,请运行
$ date --date=" 3/12/2019 "
Sample Output
样本输出
使用–date选项显示更早的日期 (Using –date option to display earlier dates)
The --date
option can also be used to display past dates. Simply pass a string in the arguments and the date will be converted in digital form.
--date
选项还可以用于显示过去的日期。 只需在参数中传递一个字符串,日期就会以数字形式转换。
For example, to display the date 10 days ago, use the syntax
例如,要显示10天前的日期,请使用以下语法
date --date=" 10 day ago "
Sample Output
样本输出
To display the date 5 years ago run.
要显示5年前的运行日期。
date --date=" 5 year ago "
Sample Output
样本输出
To display yesterday’s date run.
显示昨天的日期运行。
date --date=" yesterday "
Sample Output
样本输出
Let’s now see how you can display future dates.
现在让我们看看如何显示将来的日期。
使用–date选项显示将来的日期 (Using –date option to display future dates)
Displaying future dates is easy and is similar to the previous example. You simply need to define the string of the future date in the argument.
显示将来的日期很容易,并且与前面的示例相似。 您只需要在参数中定义将来日期的字符串。
For instance, to display tomorrow’s date run.
例如,显示明天的日期运行。
date --date=" tomorrow "
Sample Output
样本输出
To display next Friday’s date and time run:
要显示下周五的日期和时间,请运行:
date --date=" next fri "
Sample Output
样本输出
To display date and time after 2 years from the current date run:
要显示从当前日期起两年后的日期和时间:
date --date=" 2 year "
Sample Output
样本输出
设定系统日期和时间 (Setting System date and time)
Apart from displaying system date and time, date command can be used to change the date and time of your Unix/Linux system. You can achieve this using the --set
option as shown in the example below.
除了显示系统日期和时间之外,date命令还可以用于更改Unix / Linux系统的日期和时间。 您可以使用--set
选项来实现此目的,如下例所示。
Syntax
句法
$ date --set="date to be set"
For example,
例如,
$ date --set="Mon July 8 11:54:50 EAT 2019"
Sample Output
样本输出
显示文件的最后修改时间戳 (Display last modified timestamp of a file)
To check the last time a file was modified, use the -r
option as shown.
要检查文件的最后修改时间,请使用-r
选项,如图所示。
$ date -r
Sample Output
样本输出
与date命令一起使用的格式说明符 (Format specifiers used with date command)
Before we wrap up, here is a list of format options you can use to define the output of date command.
在总结之前,这是可用于定义date命令输出的格式选项列表。
%D: Display date as mm/dd/yy.
%d: Display the day of the month (01 to 31).
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of the year (01 to 12).
%y: Displays the last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24-hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.
The syntax of using these options is as follows.
使用这些选项的语法如下。
$date "+%[format-option]"
For example to display date as mm/dd/yy run:
例如,将日期显示为年/月/日:
$date "+%D"
Sample Output
样本输出
To display the day of the week in a string format run:
要以字符串格式显示星期几,请运行:
$date "+%a"
Sample Output
样本输出
And so on and so forth.
等等等等。
You have come to the end of this guide. We hope it has been helpful to you and we believe that you are now comfortable using the date command. Your feedback is welcome.
您到了本指南的结尾。 我们希望它对您有所帮助,并且相信您现在可以轻松地使用date命令。 欢迎您提供反馈。
翻译自: https://www.journaldev.com/31496/linux-date-command-examples
linux date命令