文章中实例都是基于CentOS7
date学习–系统使用的是中文版的CentOS7
Linux两种时钟
系统时钟–由Linux内核通过CPU的工作频率进行的计时
硬件时钟
#hwclock:显示硬件时钟
#-s:设置系统时钟同硬件时钟
#-w:设置硬件时钟同系统时钟
#显示日历
[root@director ~]# cal
August 2021
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
[root@lotus ~]# locale
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
date
显示当前日期时间
[root@lotus ~]# date
2020年 12月 05日 星期六 21:55:34 CST
-d 根据提供的日期显示设定的时间
[root@lotus ~]# date -d '2010-01-01'
2010年 01月 01日 星期五 00:00:00 CST
-f 将文件中的每一行日期时间数据格式化成date的格式
[root@lotus ~]# cat abc
2020-01-01
2020-08-09
[root@lotus ~]# date -f abc
2020年 01月 01日 星期三 00:00:00 CST
2020年 08月 09日 星期日 00:00:00 CST
-r 显示文件的最后修改时间
[root@lotus ~]# date -r abc
2020年 12月 05日 星期六 21:58:35 CST
-R根据RFC822指定格式显示日期时间
[root@lotus ~]# date -R
Sat, 05 Dec 2020 22:01:57 +0800
-s设定当前电脑的日期时间
[root@lotus ~]# date -s 2020-01-01
2020年 01月 01日 星期三 00:00:00 CST
更新本地系统时间
[root@lotus ~]# ntpdate cn.pool.ntp.org
5 Dec 22:05:18 ntpdate[6244]: step time server 84.16.73.33 offset 29368993.395878 sec
[root@lotus ~]# date
2020年 12月 05日 星期六 22:05:23 CST
-u 显示和设置全球时间(格林威治)
[root@lotus ~]# date -u
2020年 12月 05日 星期六 14:06:26 UTC
–version 显示date版本信息
[root@lotus ~]# date --version
date (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。
由David MacKenzie 编写。
+%a显示周几
[root@lotus ~]# date +%a
六
+%A显示完整的星期表示形式
[root@lotus ~]# date +%A
星期六
+%b显示当前区域的月份
[root@lotus ~]# date +%b
12月
+%c 当前区域的日期和时间 (Sat Nov 04 12:02:33 EST 1989)
[root@lotus ~]# date +%c
2020年12月05日 星期六 22时31分30秒
+%d (月份中的)几号(用两位表示) (01…31)
[root@lotus ~]# date +%d
05
+%D 日期(按照 月/日期/年 格式显示) (mm/dd/yy)
[root@lotus ~]# date +%D
12/05/20
+%e (月份中的)几号(去零表示) ( 1…31)
[root@lotus ~]# date +%e
5
+%h 同 %b
[root@lotus ~]# date +%h
12月
+%H 小时(按 24 小时制显示,用两位表示) (00…23)
[root@lotus ~]# date +%H
22
+%I 小时(按 12 小时制显示,用两位表示) (01…12)
[root@lotus ~]# date +%l
10
+%j (一年中的)第几天(用三位表示) (001…366)
[root@lotus ~]# date +%j
340
+%k 小时(按 24 小时制显示,去零显示) ( 0…23)
[root@lotus ~]# date +%k
22
+%l 小时(按 12 小时制显示,去零表示) ( 1…12)
[root@lotus ~]# date +%I
10
+%m 月份(用两位表示) (01…12)
[root@lotus ~]# date +%m
12
+%M 分钟数(用两位表示) (00…59)
[root@lotus ~]# date +%M
47
+%n 换行
[root@localhost ~]# date +"This year is %Y%nToday is %d"
This year is 2020
Today is 28
+%p 当前时间是上午 AM 还是下午 PM
[root@lotus ~]# date +%p
下午
+%r 时间,按 12 小时制显示 (hh:mm:ss [A/P]M)
[root@lotus ~]# date +%r
下午 10时49分57秒
+%s 从 1970年1月1日0点0分0秒到现在历经的秒数 (GNU扩充)
[root@lotus ~]# date +%s
1607179911
+%S 秒数(用两位表示)(00…60)
[root@lotus ~]# date +%S
01
+%t 水平方向的 tab 制表符
[root@lotus ~]# date +%Y%t%m%t%d
2020 12 05
+%T 时间,按 24 小时制显示(hh:mm:ss)
[root@lotus ~]# date +%T
22:55:35
+%U (一年中的)第几个星期,以星期天作为一周的开始(用两位表示)(00…53)
[root@lotus ~]# date +%U
48
+%V (一年中的)第几个星期,以星期一作为一周的开始(用两位表示)(01…52)
[root@lotus ~]# date +%V
49
+%w 用数字表示星期几 (0…6); 0 代表星期天
[root@lotus ~]# date +%w
6
+%W (一年中的)第几个星期,以星期一作为一周的开始(用两位表示)(00…53)
[root@lotus ~]# date +%W
48
+%x 按照 (mm/dd/yy) 格式显示当前日期
[root@lotus ~]# date +%x
2020年12月05日
+%X 按照 (%H:%M:%S) 格式显示当前时间
[root@lotus ~]# date +%X
23时05分42秒
[root@lotus ~]# date +%H:%M:%S
23:06:52
+%y 年的后两位数字 (00…99)
[root@lotus ~]# date +%y
20
+%Y 年(用 4 位表示) (1970…)
[root@lotus ~]# date +%Y
2020
+%z 按照 RFC-822 中指定的数字时区显示(如, -0500) (为非标准扩充)
[root@lotus ~]# date +%z
+0800
+%Z 时区(例如, EDT (美国东部时区)), 如果不能决定是哪个时区则为空
默认情况下,用 0 填充数据的空缺部分. GNU 的 date 命令能分辨在
[root@lotus ~]# date +%Z
CST
`%'和数字指示之间的以下修改.
-' (连接号) 不进行填充
_’ (下划线) 用空格进行填充
[root@lotus ~]# date +%Y-%m-%d
2020-12-05
[root@lotus ~]# date +%Y_%m_%d
2020_12_05
如何查看2020-12-06是星期几
[root@lotus ~]# date -d '2020-12-06' +%a
日
[root@lotus ~]# date -d '2020-12-06' +%A
星期日