子嘉的博客 子嘉的博客
首页
bic-bic
技术
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

高子嘉

没有比脚更长的路,没有比人更高的山
首页
bic-bic
技术
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 技术文档

  • GitHub技巧

  • 博客搭建

  • 服务端

  • distributed

  • golang

  • db

  • docker

  • linux

    • directory
    • directory
    • user
    • shell

      • dollar
      • nc
      • signal
      • kill
      • find
      • date
      • chmod
  • 技术
  • linux
  • shell
子嘉
2022-06-05

date

命令参数

-d, --date=STRING display time described by STRING, not `now' -f, --file=DATEFILE like --date once for each line of DATEFILE -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
--help display this help and exit
--version output version information and exit
1
2
3
4
5
6
7
8

主要参数如下:

-d 显示字符串所指的日期与时间。字符串前后必须加上双引号
-s 根据字符串来设置日期与时间。字符串前后必须加上双引号
-r 显示文件的最后修改时间
-u 显示 UTC 格式时间
1
2
3
4

日期格式字符串,要使用%,需要转义,即使用%%。其他字符串可以使用 date --h 查看

%% a literal %
%d day of month (e.g, 01)
%F full date; same as %Y-%m-%d
%H hour (00..23)
%I hour (01..12)
%m month (01..12)
%M minute (00..59)
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%T time; same as %H:%M:%S
%Y year
1
2
3
4
5
6
7
8
9
10
11

date 用的比较多的地方是根据当前的机器时间创建新文件或记录日志。 例,显示当前时间

date +"%Y-%m-%d %H:%M:%S" 2019-03-15 14:36:37

例,显示当前时间

date +"%F %T" 2019-03-15 14:36:37

例,显示当前时间距离 1970 年 1 月 1 日的秒数

date +"%s" 1552632375

date -d 的一些参数
date -d "2019-1-1" 直接显示字符串的时间
date -d "+1 day" 显示当前时间 +1 天的时间
date -d "-1 day" 显示当前时间 -1 天的时间
date -d "+1 month 1991-1-1" 显示 1991-1-1 +1 月表示的时间
date -d "-1 month 1991-1-1" 显示 1991-1-1 -1 月表示的时间
date -d "+1 minute 1991-1-1" 显示 1991-1-1 +1 分表示的时间
date -d "-1 minute 1991-1-1" 显示 1991-1-1 -1 分表示的时间
date -d "+1 seconds 1991-1-1" 显示 1991-1-1 -1 秒表示的时间
date -d "-1 seconds 1991-1-1" 显示 1991-1-1 -1 秒表示的时间
1
2
3
4
5
6
7
8
9
10

这里 date -d 后面的参数可以使用单数或复数形式,即使用 year 和 years 是相同的 例,将秒数解析成当前时间

date -d "1970-01-01 1554976382 seconds" +"%F %T" 2019-03-15 06:46:15

例,格式化显示指定日期字符串

date -d "2019-12-26" +"%F %T" 2019-12-26 00:00:00

使用 date -s 设置当前系统时间或日期

date -s 20190316 把时间设置为 20190316 00:00:00
date -s 22:46:00 设置时间,日期不变
date -s "22:46:00 2019-03-16" 设置时间日期
date -s "22:46:00 20190316" 设置时间日期
date -s "20190316 22:46:00" 设置时间日期
date -s "2019-03-16 22:46:00" 设置时间日期
1
2
3
4
5
6

例

date +"now time is:%F %T" now time is:2019-03-16 22:48:39

编辑 (opens new window)
上次更新: 2024/03/02, 18:30:15
find
chmod

← find chmod→

最近更新
01
mongodb restore
03-06
02
pytesseract
02-28
03
consul
02-24
更多文章>
Theme by Vdoing | Copyright © 2022-2025 子嘉 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式