Linux date command

Linux 命令大全Linux Command Collection

Linux dateThe command can be used to display or set the system date and time.

Syntax

date [OPTION]... [+FORMAT]
date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] [--version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]

Optional parameters

  • -d, --date=STRING: Display the time format through a string; the string cannot be 'now'.
  • -f, --file=DATEFILE: Similar to --date; process one line at a time from DATEFILE.
  • -I[FMT], --iso-8601[=FMT]: Output the time in ISO 8601 format. FMT can be 'date' (default), 'hours', 'minutes', 'seconds', 'ns'. It can be used to set the precision of the date and time, for example: 2006-08-14T02:34:56-0600.
  • -R, --rfc-2822: Output the time and date in RFC 5322 format, for example: Mon, 14 Aug 2006 02:34:56 -0600.
  • --rfc-3339=FMT: Output in RFC 3339 format. FMT can be one of 'date', 'seconds', 'ns'. It can be used to set the precision of the date and time, for example: 2006-08-14 02:34:56-06:00.
  • -r, --reference=FILE: Display the last modification time of the file.
  • -s, --set=STRING: Set the system time based on the string.
  • -u, --utc, --universal: Display or set Coordinated Universal Time (UTC).
  • --help: Display help information.
  • --version: Output version information.

FORMAT parameters

In terms of display, users can set the desired format. The format is set as a plus sign followed by several markers. The list of available markers is as follows:

%%   输出字符 %
%a   星期几的缩写 (Sun..Sat)
%A   星期的完整名称(Sunday..Saturday). 
%b   缩写的月份名称(例如,Jan)
%B   完整的月份名称(例如,January)
%c   本地日期和时间(例如,Thu Mar  3 23:05:25 2005)
%C   世纪,和%Y类似,但是省略后两位(例如,20)
%d   日 (01..31)
%D   日期,等价于%m/%d/%y
%e   一月中的一天,格式使用空格填充,等价于%_d
%F   完整的日期;等价于 %Y-%m-%d
%g   ISO 标准计数周的年份的最后两位数字
%G   ISO 标准计数周的年份,通常只对%V有用
%h   等价于 %b
%H   小时 (00..23)
%I   小时 (01..12)
%j   一年中的第几天 (001..366)
%k   小时,使用空格填充 ( 0..23); 等价于 %_H
%l   小时, 使用空格填充 ( 1..12); 等价于 %_I
%m   月份 (01..12)
%M   分钟 (00..59)
%n   新的一行,换行符
%N   纳秒 (000000000..999999999)
%p   用于表示当地的AM或PM,如果未知则为空白
%P   类似 %p, 但是是小写的
%r   本地的 12 小时制时间(例如 11:11:04 PM)
%R   24 小时制 的小时与分钟; 等价于 %H:%M
%s   自 1970-01-01 00:00:00 UTC 到现在的秒数
%S   秒 (00..60)
%t   插入水平制表符 tab
%T   时间; 等价于 %H:%M:%S
%u   一周中的一天 (1..7); 1 表示星期一
%U   一年中的第几周,周日作为一周的起始 (00..53)
%V   ISO 标准计数周,该方法将周一作为一周的起始 (01..53)
%w   一周中的一天(0..6),0代表星期天
%W   一年中的第几周,周一作为一周的起始(00..53)
%x   本地的日期格式(例如,12/31/99)
%X   本地的日期格式(例如,23:13:48)
%y   年份后两位数字 (00..99)
%Y   年
%z   +hhmm 格式的数值化时区格式(例如,-0400)
%:z  +hh:mm 格式的数值化时区格式(例如,-04:00)
%::z  +hh:mm:ss格式的数值化时区格式(例如,-04:00:00)
%:::z  数值化时区格式,相比上一个格式增加':'以显示必要的精度(例如,-04,+05:30)
%Z  时区缩写 (如 EDT)

If it does not start with a plus sign, it means setting the time, and the time format is MMDDhhmm[[CC]YY][.ss], whereMMis the month,DDis the day,hhis the hour,mmis the minute,CCis the first two digits of the year,YYis the last two digits of the year,ssis the number of seconds.

Permissions: All users.

When you do not want meaningless zeros to appear (for example, 1999/03/07), you can insert a - symbol in the marker, for exampledate '+%-H:%-M:%-S'It will remove meaningless zeros in hours, minutes, and seconds. For example, the original 08:09:04 will become 8:9:4. In addition, only those with permissions (such as root) can set the system time.

After you change the system time as root, remember to useclock -wto write the system time intoCMOSso that the system time will continue to maintain the latest correct value next time you reboot.

Examples

Display current time

# date
Tue May 24 09:29:43 CST 2022
# date '+%c' 
Tue 24 May 2022 09:30:03 AM CST
# date '+%D' //显示完整的时间
05/24/22
# date '+%x' //显示数字日期
05/24/2022
# date '+%T' //显示日期,年份用四位数表示
14:09:31
# date '+%X' //显示24小时的格式
09:31:31 AM

Formatted output:

# date +"%Y-%m-%d"
2009-12-07

Output yesterday's date:

# date -d "1 day ago" +"%Y-%m-%d"
2012-11-19

Output the time 2 seconds later:

# date -d "2 second" +"%Y-%m-%d %H:%M.%S"
2012-11-20 14:21.31

The legendary 1234567890 seconds:

# date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%M:%S"
2009-02-13 23:02:30

Or:

# date -d@1234567890 +"%F %T"
2009-02-13 23:02:30

Time format conversion:

# date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"
2009/12/12 00:00.00

Apache format conversion:

# date -d "Dec 5, 2009 12:00:37 AM" +"%Y-%m-%d %H:%M.%S"
2009-12-05 00:00.37

Time travel after format conversion:

# date -d "Dec 5, 2009 12:00:37 AM 2 year ago" +"%Y-%m-%d %H:%M.%S"
2007-12-05 00:00.37

Output in your own format

# date '+usr_time: $1:%M %P -hey'
usr_time: $1:16 下午 -hey

After displaying the time, output a newline, then display the current date

date '+%T%n%D'

Display the month and day

date '+%B %d'

Display the date and set the time (12:34:56)

date --date '12:34:56'

Time addition and subtraction operations:

date +%Y%m%d                   # 显示年月日
date -d "+1 day" +%Y%m%d       # 显示后一天的日期
date -d "-1 day" +%Y%m%d       # 显示前一天的日期
date -d "-1 month" +%Y%m%d     # 显示上一月的日期
date -d "+1 month" +%Y%m%d     # 显示下一月的日期
date -d "-1 year" +%Y%m%d      # 显示前一年的日期
date -d "+1 year" +%Y%m%d      # 显示下一年的日期

Set time:

date -s                         # 设置当前时间,只有root权限才能设置,其他只能查看
date -s 20120523                # 设置成20120523,这样会把具体时间设置成00:00:00
date -s 01:01:01                # 设置具体时间,不会对日期做更改
date -s "01:01:01 2012-05-23"   # 这样可以设置全部时间
date -s "01:01:01 20120523"     # 这样可以设置全部时间
date -s "2012-05-23 01:01:01"   # 这样可以设置全部时间
date -s "20120523 01:01:01"     # 这样可以设置全部时间

Linux 命令大全Linux Command Collection

Other extensions