Linux系统之date命令的基本使用
- 一、date命令介绍
- 二、检查本地系统环境
- 2.1 检查系统版本
- 2.2 检查yum仓库
 
- 三、date命令的使用帮助
- 3.1 date命令的帮助信息
- 3.2 date命令参数解释
- 3.3 date命令常用的选项
 
- 四、date命令的日常使用
- 4.1 直接显示时间
- 4.2 格式化输出时间
- 4.3 时间格式转换
- 4.4 设定时间
- 4.5 手动设置时间
- 4.6 恢复正确的系统本地时间
 
一、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 dateand 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' fordate and time to the indicated precision.Date and time components are separated bya 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 exitFORMAT 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 spaces0  (zero) pad with zeros^  use upper case if possible#  use opposite case if possibleAfter 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' dateShow 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)
%😒 +hh:mm格式的数值化时区格式(例如,-04:00)
%:😒 +hh:mm:ss格式的数值化时区格式(例如,-04:00:00)
%::😒 数值化时区格式,相比上一个格式增加’:'以显示必要的精度(例如,-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:104.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 20214.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