GNU/Linux - date命令

Linux 中的日期命令是一个多功能的强大工具,用于显示和设置系统的日期和时间。它允许用户以各种格式检索当前日期和时间,自定义输出,甚至出于脚本、日志和系统维护等不同目的操作日期和时间值。

下面是日期命令的简要概述:

The date command in Linux is a versatile and powerful tool used to display and set the system's date and time. It allows users to retrieve the current date and time in various formats, customize the output, and even manipulate date and time values for different purposes such as scripting, logging, and system maintenance.

Here’s a brief overview of the date command:

Basic Usage

显示当前日期和时间:

只需在终端中输入date,就能以默认格式输出当前系统日期和时间。

Display Current Date and Time: 

Simply typing "date" in the terminal will output the current system date and time in the default format.

$ date

Tue May 21 12:34:56 UTC 2024

Formatting Output

自定义日期格式:

"date"命令支持多种格式参数,允许用户以自定义方式显示日期和时间。例如,以 YYYY-MM-DD 格式显示日期:

Custom Date Formats:

The date command supports a variety of format specifiers, allowing users to display the date and time in a customized manner. For example, to display the date in YYYY-MM-DD format:

语法:

date +%[format-option]

date "+%[format-option]"

$ date "+%Y-%m-%d"

2024-05-21

Common format specifiers include:

%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 year (01 to 12).

%y: Displays 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. (00-23)

%M: Display the minute. (00-59)

%S: Display the seconds. (00-59)

更多信息可以使用man date查询。 

How to Display Last Modified Timestamp of a File / 如何显示文件的最后修改时间戳

date -r file.txt

How to Set the System Date and Time / 如何设置系统日期和时间

使用 -s 或 --set 选项。需要管理员权限(superuser privileges)。

date --set="date to be set"

For instance, to set the date to June 15, 2024, 12:23 AM:

$ sudo date 061512232024

1、只改系统的日期,不改时间       date -s 2012-08-02

2、只改时间不改日期 date -s 10:08:00

3、同时修改日期和时间   date -s "2012-05-18 04:53:00"

注意:由于日期和时间之间有空格,所以必须用引号引起来,否则命令会报错。

Use Unix Epoch Time (Epoch Converter) / 使用 Unix 纪元时间(纪元转换器)

您可以将日期命令用作纪元转换器。纪元或 Unix 时间戳是指从 1970 年 1 月 1 日 00:00:00 UTC 开始经过的秒数。

要显示从历元到当前日期的秒数,请使用 %s 格式控制:

You can use the date command as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.

To show the number of seconds from the epoch to the current day, use the %s format control:

$ date +%s

1716372039

# 使用纪元时间设置日期

$ date -s @1716372039

要查看从纪元到特定日期之间过去了多少秒,请输入:

To see how many seconds passed from epoch to a specific date, enter:

$ date -d "1984-04-08" +"%s"

How to Display Time in GMT

使用 date 命令的 -u 选项,可以用格林威治标准时间(GMT)或协调世界时(UTC)显示当前时间。

The -u option with the date command allows you to display the current time in GMT (Greenwich Mean Time) or UTC (Coordinated Universal Time).

date -u

$ date -u

2024年 05月 21日 星期二 09:44:18 UTC

$ date

2024年 05月 21日 星期二 17:44:23 CST

使用UTC时间显示Linux纪元时间:

$ date -u -d @1718468837

2024年 06月 15日 星期六 16:27:17 UTC

$ date -d @1718468837

2024年 06月 15日 星期六 12:27:17 EDT

$date -d @1718468837 +%4Y%2m%2d%2H%2M%2S

20240615122717

How to Display the Given Date String in the Format of Date / 如何以日期格式显示给定的日期字符串

$date --date="2/02/2010" or $date -d "2/02/2010"

$date --date="Feb 2 2010"

Output:

Tue Feb  2 00:00:00 PST 2010

Tue Feb  2 00:00:00 PST 2010

#以系统当前的日期格式显示给定的日期和时间

$date -d "2000-11-22 09:10:15"  

How to Display Past Dates

date --date="2 year ago"

date --date="5 sec ago"

date --date="yesterday"

date --date="2 month ago"

date --date="10 day ago"

date -d "+7 days"

How to Display Future Dates

$date --date="next tue"

$date --date="2 day"

$date --date="tomorrow"

$date --date="1 year"

参考:

How to Display and Set Date and Time in Linux | date Command - GeeksforGeeks

Date Command in Linux: How to Set, Change, Format and Display Date

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值