Date
打印或者设置系统时间
常用于日志文件、日期目录等的生成
语法格式
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
参数(option)
-d, --date=STRING
display time described by STRING, not 'now'
-s, --set=STRING
set time described by STRING
格式化
格式 | 含义 |
---|---|
%Y | year |
%y | last two digits of year (00..99) |
%m | month (01..12) |
%d | day of month (e.g., 01) |
%H | hour (00..23) |
%I | hour (01..12) |
%M | minute (00..59) |
%S | second (00..60) |
%N | nanoseconds (000000000..999999999) |
%w | day of week (0..6); 0 is Sunday |
%W | week number of year, with Monday as first day of week (00..53) |
%s | seconds since 1970-01-01 00:00:00 UTC |
%F | full date; same as %Y-%m-%d |
%T | time; same as %H:%M:%S |
注意事项
默认情况下,日期以0填充,比如2018年9月9日会表示成2018-09-09
%后添加下面可选参数,实现自定义补全
- -,(hyphen) do not pad the field
- _,(underscore) pad with spaces
- 0,(zero) pad with zeros
示例
1 不使用自动补全
# date
Sun Sep 9 02:04:26 CST 2018
# date "+%Y-%-m-%-d %-H:%-M:%-S"
2018-9-9 2:4:11
2 显示3天前的日期
# date "+%F %T" -d "-3 day"
2018-09-06 02:14:43
3 时间戳转日期
注意:从epoch(1970-01-01 UTC)截至当前日期的秒,而非微秒;若是微秒级,需除以1000,再做转换
# date "+%s"
1536430623
date -d @1536430623
Sun Sep 9 02:17:03 CST 2018
4 设置时间
# date -s "20181029 11:42:33"
Mon Oct 29 11:42:33 CST 2018
# date -s "2018/10/29 11:42:33"
Mon Oct 29 11:42:33 CST 2018
date -s "2018-10-9 11:42:33"
Tue Oct 9 11:42:33 CST 2018
网友评论