date命令用法
- date +%Y-%m-%d, date +%y-%m-%d 年月日
- date +%H:%M:%S = date +%T 时间
- date +%s 时间戳
- date -d @1504620492
- date -d "+1day" 一天后
- date -d "-1 day" 一天前
- date -d "-1 month" 一月前
- date -d "-1 min" 一分钟前
- date +%w, date +%W 星期
date命令用法
- date命令,会显示当前系统时间日期
[root@hf-01 ~]# date
2018年 01月 14日 星期日 06:13:14 CST
[root@hf-01 ~]#
- date命令,在shell中用处很是大;对文件后缀增长一个时间,以便后期管理
- date +%Y-%m-%d, date +%y-%m-%d 年月日
[root@hf-01 ~]# LANG=en 切换为英文显示
[root@hf-01 ~]# date
Sun Jan 14 06:19:49 CST 2018
[root@hf-01 ~]# date +%Y
2018 四位的年
[root@hf-01 ~]# date +%y
18 两位的年
[root@hf-01 ~]# date +%m
01 月份
[root@hf-01 ~]# date +%M
20 分钟
[root@hf-01 ~]# date +%d
14 日期
[root@hf-01 ~]# date +%D
01/14/18 直接标记年月日,不过格式比较特殊
[root@hf-01 ~]# date +%Y%m%d
20180114 年月日
[root@hf-01 ~]# date +%F
2018-01-14 年月日,这种带横杠的
[root@hf-01 ~]#
- 常见时间单位
[root@hf-01 ~]# date +%w
0 表示周几
[root@hf-01 ~]# date +%W
02 今年的第几周,今年的第二周
[root@hf-01 ~]# date +%h
Jan 英文的月份
[root@hf-01 ~]# date +%H
06 小时
[root@hf-01 ~]# date +%S
04 秒
[root@hf-01 ~]# date +%s
1515882702 这是一个时间戳,距离1970总共过去多少秒
- 时间其余标记方法
- date +%H:%M:%S = date +%T 时间
[root@hf-01 ~]# date +%T
06:24:36
[root@hf-01 ~]# date +%H:%M:%S
06:24:36
[root@hf-01 ~]#
- 显示日历 cal命令,查看到日期
[root@hf-01 ~]# cal
January 2018
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@hf-01 ~]#
- 标记以前的日期
- 好比:在作nginx日志切割的时候,到了凌晨切割日志,到了零点零分切割的日志是前一天的日志。因此把日志加一个时间标记的话,应标记为昨天的日期
- 学会用date标记以前的日期
- day、month、year、hour、min后面能够加 s 能够不加 s
- 减号- 表示以前的日期,加号 + 表示从今日后的日期
- date -d "-1 day" +%F 显示前一天的日期
- date -d "-1 month" +%F 显示上个月的日期
- date -d "-1 years" +%F 显示上一年的日期
- date -d "+1 hour" +%T 显示下一小时
- date -d "+1 min" +%T 显示下一分钟
[root@hf-01 ~]# date -d "-1 day"
Sat Jan 13 06:47:30 CST 2018
[root@hf-01 ~]# date -d "-1 day" +%F
2018-01-13
[root@hf-01 ~]# date -d "+1 month" +%F
2018-02-14
[root@hf-01 ~]# date -d "+1 year" +%F
2019-01-14
[root@hf-01 ~]# date -d "+1 hour" +%T
08:09:05
[root@hf-01 ~]# date -d "+1 min" +%T
07:11:21
- 时间戳
- date +%s
- 另外一种表现方法,表示时间戳
- date -d @1504620492 就是@后跟时间戳
[root@hf-01 ~]# date +%s
1515885248
[root@hf-01 ~]# date -d @1515885248
Sun Jan 14 07:14:08 CST 2018
[root@hf-01 ~]#
- 若想在linux系统中,把具体的日期换算成时间戳的时候,能够使用date +%s -d "2018-01-13 07:14:08"
[root@hf-01 ~]# date +%s -d "2018-01-13 07:14:08"
1515798848
[root@hf-01 ~]# date -d @1515798848
Sat Jan 13 07:14:08 CST 2018
[root@hf-01 ~]#