如何在终端中获取当前日期和时间,并在终端设置一个自定义命令?

时间:2023-02-05 09:02:23

I have to check the time in linux terminal.
What is the command for getting date and time in Linux terminal ?
Is there any way in which we can set custom function ?

我必须检查一下linux终端的时间。在Linux终端中获取日期和时间的命令是什么?有什么方法可以设置自定义函数吗?

2 个解决方案

#1


706  

The command is date

命令的日期

To customise the output there are a myriad of options available, see date --help for a list.

要定制输出,有大量可用的选项,见日期——帮助列表。

For example, date '+%A %W %Y %X' gives Tuesday 34 2013 08:04:22 which is the name of the day of the week, the week number, the year and the time.

例如,date '+% % %W %Y %X'给出了周二的日期,即一周的日期、星期号、年份和时间。

#2


52  

You can use date to get time and date of a day:

你可以使用日期来获得一天的时间和日期:

[pengyu@GLaDOS ~]$date
Tue Aug 27 15:01:27 CST 2013

Also hwclock would do:

还hwclock将做的事:

[pengyu@GLaDOS ~]$hwclock
Tue 27 Aug 2013 03:01:29 PM CST  -0.516080 seconds

For customized output, you can either redirect the output of date to something like awk, or write your own program to do that.

对于定制的输出,您可以将日期的输出重定向到awk之类的东西,或者编写您自己的程序来实现这一点。

Remember to put your own executable scripts/binary into your PATH (e.g. /usr/bin) to make it invokable anywhere.

记得把你自己的可执行脚本/二进制文件放到你的路径中(例如/usr/bin),让它在任何地方都可以调用。

However, I don't think this is a good question for *.

然而,我认为这不是*的一个好问题。

#1


706  

The command is date

命令的日期

To customise the output there are a myriad of options available, see date --help for a list.

要定制输出,有大量可用的选项,见日期——帮助列表。

For example, date '+%A %W %Y %X' gives Tuesday 34 2013 08:04:22 which is the name of the day of the week, the week number, the year and the time.

例如,date '+% % %W %Y %X'给出了周二的日期,即一周的日期、星期号、年份和时间。

#2


52  

You can use date to get time and date of a day:

你可以使用日期来获得一天的时间和日期:

[pengyu@GLaDOS ~]$date
Tue Aug 27 15:01:27 CST 2013

Also hwclock would do:

还hwclock将做的事:

[pengyu@GLaDOS ~]$hwclock
Tue 27 Aug 2013 03:01:29 PM CST  -0.516080 seconds

For customized output, you can either redirect the output of date to something like awk, or write your own program to do that.

对于定制的输出,您可以将日期的输出重定向到awk之类的东西,或者编写您自己的程序来实现这一点。

Remember to put your own executable scripts/binary into your PATH (e.g. /usr/bin) to make it invokable anywhere.

记得把你自己的可执行脚本/二进制文件放到你的路径中(例如/usr/bin),让它在任何地方都可以调用。

However, I don't think this is a good question for *.

然而,我认为这不是*的一个好问题。