Linux系统之date命令的基本使用

简介: Linux系统之date命令的基本使用

@TOC

一、date命令介绍

  • date命令用于显示或设置系统的日期和时间。

二、检查本地系统环境

2.1 检查系统版本

检查本地操作系统版本,此次使用的是centos7.6。

[root@jeven ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

2.2 检查yum仓库

检查yum仓库各镜像源的状态

[root@jeven ~]# yum repolist all |grep enable 
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
!base/7/x86_64        CentOS-7 - Base - mirrors.aliyun.com       enabled: 10,072
!extras/7/x86_64      CentOS-7 - Extras - mirrors.aliyun.com     enabled:    515
!updates/7/x86_64     CentOS-7 - Updates - mirrors.aliyun.com    enabled:  4,996

三、date命令的使用帮助

3.1 date命令的帮助信息

date命令在Linux中的help帮助信息。

[root@jeven ~]# date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING         display time described by STRING, not 'now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
  -I[TIMESPEC], --iso-8601[=TIMESPEC]  output date/time in ISO 8601 format.
                            TIMESPEC='date' for date only (the default),
                            'hours', 'minutes', 'seconds', or 'ns' for date
                            and time to the indicated precision.
  -r, --reference=FILE      display the last modification time of FILE
  -R, --rfc-2822            output date and time in RFC 2822 format.
                            Example: Mon, 07 Aug 2006 12:34:56 -0600
      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                            TIMESPEC='date', 'seconds', or 'ns' for
                            date and time to the indicated precision.
                            Date and time components are separated by
                            a single space: 2006-08-07 12:34:56-06:00
  -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

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; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); 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
  %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 1970-01-01 00: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 representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (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
  ^  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'

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'date invocation'

3.2 date命令参数解释

format:输出的时间格式。

format可用的转义序列如下:

%% 百分号
%a 当地缩写的工作日名称(例如,Sun)
%A 当地完整的工作日名称(例如,Sunday)
%b 当地缩写的月份名称(例如,Jan)
%B 当地完整的月份名称(例如,January)
%c 当地的日期和时间(例如,Thu Mar 3 23:05:25 2005)
%C 世纪,和%Y类似,但是省略后两位(例如,20)
%d 一月中的一天(例如,01)
%D 日期,等价于%m/%d/%y
%e 一月中的一天,格式使用空格填充,等价于%_d
%F 完整的日期;等价于%+4Y-%m-%d
%g ISO标准计数周的年份的最后两位数字
%G ISO标准计数周的年份,通常只对%V有用
%h 等价于%b
%H 小时,范围(00..23)
%I 小时,范围(00..23)
%j 一年中的一天,范围(001..366)
%k 小时,使用空格填充,范围(0..23),等价于%_H
%l 小时,使用空格填充,范围(1..12),等价于%_I
%m 月,范围(01..12)
%M 分钟,范围(00..59)
%n 换行符
%N 纳秒,范围(000000000..000000000)
%p 用于表示当地的AM或PM,如果未知则为空白
%P 类似于%p,但用小写表示
%q 季度,范围(1..4)
%r 当地以12小时表示的时钟时间(例如,11:11:04 PM)
%R 24小时每分钟;等价于%H:%M
%s 自协调世界时1970年01月01日00时00分以来的秒数
%S 秒数,范围(00..60)
%t 水平制表符
%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)

3.3 date命令常用的选项

长选项与短选项等价

-d, --date=STRING          解析字符串并按照指定格式输出,字符串不能是'now'。
--debug                    注释已解析的日期,并将有疑问的用法发送到标准错误。
-f, --file=DATEFILE        类似于--date; 一次从DATEFILE处理一行。
-I[FMT], --iso-8601[=FMT]  按照ISO 8601格式输出,FMT可以为'date'(默认)'hours''minutes''seconds''ns'。
                           例如:2006-08-14T02:34:56-06:00
-R, --rfc-email            按照RFC 5322格式输出,例如: Mon, 14 Aug 2006 02:34:56 -0600
--rfc-3339=FMT             按照RFC 3339格式输出,FMT可以为'date', 'seconds','ns'中的一个,
                           例如:2006-08-14 02:34:56-06:00
-r, --reference=FILE       显示文件的上次修改时间。
-s, --set=STRING           根据字符串设置系统时间。
-u, --utc, --universal     显示或设置世界协调时(UTC)。
--help                     显示帮助信息并退出。
--version                  显示版本信息并退出。
%Y:年份,如2021
%m:月份,如02
%d:日期,如14
%H:小时,如14
%M:分钟,如26
%S:秒数,如12

四、date命令的日常使用

4.1 直接显示时间

直接使用date命令查看当前时间

[root@jeven ~]# date
Wed Jul 12 19:51:51 CST 2023

4.2 格式化输出时间

  • 使用以下命令格式化输出时间
[root@jeven ~]# date +%Y-%m-%d
2023-07-12
  • 指定格式显示当前具体时间
[root@jeven ~]# date "+%Y-%m-%d %H:%M:%S"
2023-07-12 19:57:10

4.3 时间格式转换

使用-d选项,进行时间格式转换

[root@jeven ~]# date -d "2012-12-12" +"%Y/%m/%d %H:%M.%S"
2012/12/12 00:00.00
  • 传说中的 1234567890 秒
[root@jeven ~]# date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%M:%S"
2009-02-13 23:31:30

4.4 设定时间

使用-s选项设置当前时间

[root@jeven ~]# date -s "2012-12-12 12:12:12" 
Wed Dec 12 12:12:12 CST 2012
[root@jeven ~]# date
Wed Dec 12 12:12:15 CST 2012

4.5 手动设置时间

直接date手动设置时间,格式为月、日、时、分、年.秒。

[root@jeven ~]#  date 090621282021.28
Mon Sep  6 21:28:28 CST 2021

4.6 恢复正确的系统本地时间

使用 chronyc命令快速同步时间,恢复正确的本地时间。

[root@jeven ~]# chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 203.107.6.88                  2   6    17     9   +224us[+2606us] +/-   24ms
[root@jeven ~]# date "+%Y-%m-%d %H:%M:%S"
2023-07-12 20:17:19
相关文章
|
2天前
|
IDE Linux 开发工具
Linux 系统上安装
在Linux和Mac上安装Lua 5.3.0只需下载源码,解压,编译和安装。Windows用户可选择SciTE IDE或通过LuaForWindows在Github或Google Code下载安装。创建 HelloWorld.lua,使用`lua HelloWorld.lua`运行显示&quot;Hello World!&quot;。另可参考LuaDist官方推荐方式安装。
|
2天前
|
安全 网络协议 Linux
linux必学的60个命令
Linux是一个功能强大的操作系统,提供了许多常用的命令行工具,用于管理文件、目录、进程、网络和系统配置等。以下是Linux必学的60个命令的概览,但请注意,这里可能无法列出所有命令的完整语法和选项,仅作为参考
193 2
|
1天前
|
监控 JavaScript 网络协议
Linux系统之安装uptime-kuma服务器监控面板
【5月更文挑战第12天】Linux系统之安装uptime-kuma服务器监控面板
10 0
|
2天前
|
Java Unix Linux
【JavaEE】Linux基本使用(下)
【JavaEE】Linux基本使用
10 0
|
2天前
|
Ubuntu Java Linux
【JavaEE】Linux基本使用(上)
【JavaEE】Linux基本使用
8 0
|
2天前
|
Linux 程序员 计算机视觉
【linux 学习】在Linux中经常用到的cmake、make、make install等命令解析
【linux 学习】在Linux中经常用到的cmake、make、make install等命令解析
15 0
|
2天前
|
Linux
Linux的find命令使用
【5月更文挑战第11天】Linux的find命令使用
14 3
|
2天前
|
Linux Perl
Linux系统的文本处理
Linux系统的文本处理
|
2天前
|
Linux API
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
|
2天前
|
监控 Linux 数据处理