本文转自:http://www.cnblogs.com/kerrycode/p/3427617.html

命令简介:

date 根据给定格式显示日期或设置系统日期时间。print or set the system date and time

指令所在路径:/bin/date

命令语法:

date [OPTION]... [+FORMAT]

date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

命令参数:

参数

描述

-d

显示字符串描述的时间

-f

显示DATEFILE文件中的每行时间

-r

显示文件的最后修改时间

-R

以RFC-2822兼容日期格式显示时间

-rfc-2822

以RFC-2822兼容日期格式显示时间

-s

设置时间为string

-u

显示或设定为Coordinated Universal Time时间格式

--help

显示date命令的帮助信息

--version

显示date命令的版本信息

Format参数格式

要说写这位程序的 David MacKenzie老兄,真是事无巨细啊,居然整了这么多格式参数,佩服佩服。

参数

描述

%%

显示字符%

%a

星期几的缩写(Sun..Sat)

%A

星期几的完整名称(Sunday...Saturday)

%b

月份的缩写(Jan..Dec)

%B

月份的完整名称(January..December)

%c

日期与时间。只输入date指令也会显示同样的结果

%C

世纪(年份除100后去整) [00-99]

%d

日期(以01-31来表示)。

%D

日期(含年月日)。

%e

一个月的第几天 ( 1..31)

%F

日期,同%Y-%m-%d

%g

年份(yy)

%G

年份(yyyy)

%h

同%b

%H

小时(00..23)

%I

小时(01..12)

%j

一年的第几天(001..366)

%k

小时( 0..23)

%l

小时( 1..12)

%m

月份(01..12)

%M

分钟(00..59)

%n

换行

%N

纳秒(000000000..999999999)

%p

AM or PM

%P

am or pm

%r

12小时制时间(hh:mm:ss [AP]M)

%R

24小时制时间(hh:mm)

%s

从00:00:00 1970-01-01 UTC开始的秒数

%S

秒(00..60)

%t

制表符

%T

24小时制时间(hh:mm:ss)

%u

一周的第几天(1..7); 1 表示星期一

%U

一年的第几周,周日为每周的第一天(00..53)

%V

一年的第几周,周一为每周的第一天 (01..53)

%w

一周的第几天 (0..6); 0 代表周日

%W

一年的第几周,周一为每周的第一天(00..53)

%x

日期(mm/dd/yy)

%X

时间(%H:%M:%S)

%y

年份(00..99)

%Y

年份 (1970…)

%z

RFC-2822 风格数字格式时区(-0500)

%Z

时区(e.g., EDT), 无法确定时区则为空

   

使用示例:

1: 查看date命令帮助信息

   1: [root@DB-Server ~]# date --help
   2:  
   3: Usage: date [OPTION]... [+FORMAT]
   4:  
   5: or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
   6:  
   7: Display the current time in the given FORMAT, or set the system date.
   8:  
   9: -d, --date=STRING display time described by STRING, not `now'
  10:  
  11: -f, --file=DATEFILE like --date once for each line of DATEFILE
  12:  
  13: -r, --reference=FILE display the last modification time of FILE
  14:  
  15: -R, --rfc-2822 output date and time in RFC 2822 format
  16:  
  17: --rfc-3339=TIMESPEC output date and time in RFC 3339 format.
  18:  
  19: TIMESPEC=`date', `seconds', or `ns' for
  20:  
  21: date and time to the indicated precision.
  22:  
  23: -s, --set=STRING set time described by STRING
  24:  
  25: -u, --utc, --universal print or set Coordinated Universal Time
  26:  
  27: --help display this help and exit
  28:  
  29: --version output version information and exit
  30:  
  31: FORMAT controls the output. The only valid option for the second form
  32:  
  33: specifies Coordinated Universal Time. Interpreted sequences are:
  34:  
  35: %% a literal %
  36:  
  37: %a locale's abbreviated weekday name (e.g., Sun)
  38:  
  39: %A locale's full weekday name (e.g., Sunday)
  40:  
  41: %b locale's abbreviated month name (e.g., Jan)
  42:  
  43: %B locale's full month name (e.g., January)
  44:  
  45: %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
  46:  
  47: %C century; like %Y, except omit last two digits (e.g., 21)
  48:  
  49: %d day of month (e.g, 01)
  50:  
  51: %D date; same as %m/%d/%y
  52:  
  53: %e day of month, space padded; same as %_d
  54:  
  55: %F full date; same as %Y-%m-%d
  56:  
  57: %g last two digits of year of ISO week number (see %G)
  58:  
  59: %G year of ISO week number (see %V); normally useful only with %V
  60:  
  61: %h same as %b
  62:  
  63: %H hour (00..23)
  64:  
  65: %I hour (01..12)
  66:  
  67: %j day of year (001..366)
  68:  
  69: %k hour ( 0..23)
  70:  
  71: %l hour ( 1..12)
  72:  
  73: %m month (01..12)
  74:  
  75: %M minute (00..59)
  76:  
  77: %n a newline
  78:  
  79: %N nanoseconds (000000000..999999999)
  80:  
  81: %p locale's equivalent of either AM or PM; blank if not known
  82:  
  83: %P like %p, but lower case
  84:  
  85: %r locale's 12-hour clock time (e.g., 11:11:04 PM)
  86:  
  87: %R 24-hour hour and minute; same as %H:%M
  88:  
  89: %s seconds since 1970-01-01 00:00:00 UTC
  90:  
  91: %S second (00..60)
  92:  
  93: %t a tab
  94:  
  95: %T time; same as %H:%M:%S
  96:  
  97: %u day of week (1..7); 1 is Monday
  98:  
  99: %U week number of year, with Sunday as first day of week (00..53)
 100:  
 101: %V ISO week number, with Monday as first day of week (01..53)
 102:  
 103: %w day of week (0..6); 0 is Sunday
 104:  
 105: %W week number of year, with Monday as first day of week (00..53)
 106:  
 107: %x locale's date representation (e.g., 12/31/99)
 108:  
 109: %X locale's time representation (e.g., 23:13:48)
 110:  
 111: %y last two digits of year (00..99)
 112:  
 113: %Y year
 114:  
 115: %z +hhmm numeric timezone (e.g., -0400)
 116:  
 117: %:z +hh:mm numeric timezone (e.g., -04:00)
 118:  
 119: %::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
 120:  
 121: %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
 122:  
 123: %Z alphabetic time zone abbreviation (e.g., EDT)
 124:  
 125: By default, date pads numeric fields with zeroes.
 126:  
 127: The following optional flags may follow `%':
 128:  
 129: - (hyphen) do not pad the field
 130:  
 131: _ (underscore) pad with spaces
 132:  
 133: 0 (zero) pad with zeros
 134:  
 135: ^ use upper case if possible
 136:  
 137: # use opposite case if possible
 138:  
 139: After any flags comes an optional field width, as a decimal number;
 140:  
 141: then an optional modifier, which is either
 142:  
 143: E to use the locale's alternate representations if available, or
 144:  
 145: O to use the locale's alternate numeric symbols if available.
 146:  
 147: Report bugs to <bug-coreutils@gnu.org>.
 148:  
   1: [root@DB-Server ~]# man date
   2:  
   3: DATE(1) User Commands DATE(1)
   4:  
   5: NAME
   6:  
   7: date - print or set the system date and time
   8:  
   9: SYNOPSIS
  10:  
  11: date [OPTION]... [+FORMAT]
  12:  
  13: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
  14:  
  15: DESCRIPTION
  16:  
  17: Display the current time in the given FORMAT, or set the system date.
  18:  
  19: -d, --date=STRING
  20:  
  21: display time described by STRING, not ‘now’
  22:  
  23: -f, --file=DATEFILE
  24:  
  25: like --date once for each line of DATEFILE
  26:  
  27: -r, --reference=FILE
  28:  
  29: display the last modification time of FILE
  30:  
  31: -R, --rfc-2822
  32:  
  33: output date and time in RFC 2822 format
  34:  
  35: --rfc-3339=TIMESPEC
  36:  
  37: output date and time in RFC 3339 format. TIMESPEC=‘date’, ‘seconds’, or ‘ns’ for date and time to the indicated precision.
  38:  
  39: -s, --set=STRING
  40:  
  41: set time described by STRING
  42:  
  43: -u, --utc, --universal
  44:  
  45: print or set Coordinated Universal Time
  46:  
  47: --help display this help and exit
  48:  
  49: --version
  50:  
  51: output version information and exit
  52:  
  53: FORMAT controls the output. The only valid option for the second form specifies Coordinated Universal Time. Interpreted sequences are:
  54:  
  55: %% a literal %
  56:  
  57: %a locale’s abbreviated weekday name (e.g., Sun)
  58:  
  59: %A locale’s full weekday name (e.g., Sunday)
  60:  
  61: %b locale’s abbreviated month name (e.g., Jan)
  62:  
  63: %B locale’s full month name (e.g., January)
  64:  
  65: %c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
  66:  
  67: %C century; like %Y, except omit last two digits (e.g., 21)
  68:  
  69: %d day of month (e.g, 01)
  70:  
  71: %D date; same as %m/%d/%y
  72:  
  73: %e day of month, space padded; same as %_d
  74:  
  75: %F full date; same as %Y-%m-%d
  76:  
  77: %g last two digits of year of ISO week number (see %G)
  78:  
  79: %G year of ISO week number (see %V); normally useful only with %V
  80:  
  81: %h same as %b
  82:  
  83: %H hour (00..23)
  84:  
  85: %I hour (01..12)
  86:  
  87: %j day of year (001..366)
  88:  
  89: %k hour ( 0..23)
  90:  
  91: %l hour ( 1..12)
  92:  
  93: %m month (01..12)
  94:  
  95: %M minute (00..59)
  96:  
  97: %n a newline
  98:  
  99: %N nanoseconds (000000000..999999999)
 100:  
 101: %p locale’s equivalent of either AM or PM; blank if not known
 102:  
 103: %P like %p, but lower case
 104:  
 105: %r locale’s 12-hour clock time (e.g., 11:11:04 PM)
 106:  
 107: %R 24-hour hour and minute; same as %H:%M
 108:  
 109: %s seconds since 1970-01-01 00:00:00 UTC
 110:  
 111: %S second (00..60)
 112:  
 113: %t a tab
 114:  
 115: %T time; same as %H:%M:%S
 116:  
 117: %u day of week (1..7); 1 is Monday
 118:  
 119: %U week number of year, with Sunday as first day of week (00..53)
 120:  
 121: %V ISO week number, with Monday as first day of week (01..53)
 122:  
 123: %w day of week (0..6); 0 is Sunday
 124:  
 125: %W week number of year, with Monday as first day of week (00..53)
 126:  
 127: %x locale’s date representation (e.g., 12/31/99)
 128:  
 129: %X locale’s time representation (e.g., 23:13:48)
 130:  
 131: %y last two digits of year (00..99)
 132:  
 133: %Y year
 134:  
 135: %z +hhmm numeric timezone (e.g., -0400)
 136:  
 137: %:z +hh:mm numeric timezone (e.g., -04:00)
 138:  
 139: %::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
 140:  
 141: %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
 142:  
 143: %Z alphabetic time zone abbreviation (e.g., EDT)
 144:  
 145: By default, date pads numeric fields with zeroes. The following optional flags may follow ‘%’:
 146:  
 147: - (hyphen) do not pad the field _ (underscore) pad with spaces 0 (zero) pad with zeros ^ use upper case if possible # use opposite case if possible
 148:  
 149: After 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,
 150:  
 151: or O to use the locale’s alternate numeric symbols if available.
 152:  
 153: ENVIRONMENT
 154:  
 155: TZ Specifies the timezone, unless overridden by command line parameters. If neither is specified, the setting from /etc/localtime is used.
 156:  
 157: AUTHOR
 158:  
 159: Written by David MacKenzie.
 160:  
 161: REPORTING BUGS
 162:  
 163: Report bugs to <bug-coreutils@gnu.org>.
 164:  
 165: COPYRIGHT
 166:  
 167: Copyright ? 2006 Free Software Foundation, Inc.
 168:  
 169: This is free software. You may redistribute copies of it under the terms of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to
 170:  
 171: the extent permitted by law.
 172:  
 173: SEE ALSO
 174:  
 175: The full documentation for date is maintained as a Texinfo manual. If the info and date programs are properly installed at your site, the command
 176:  
 177: info date
 178:  
 179: should give you access to the complete manual.
 180:  
 181: date 5.97 May 2011 DATE(1)
 182:  

2:查看date命令的版本信息

   1: [root@DB-Server ~]# date --version
   2:  
   3: date (GNU coreutils) 5.97
   4:  
   5: Copyright (C) 2006 Free Software Foundation, Inc.
   6:  
   7: This is free software. You may redistribute copies of it under the terms of
   8:  
   9: the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
  10:  
  11: There is NO WARRANTY, to the extent permitted by law.
  12:  
  13: Written by David MacKenzie.
  14:  

3:运用-d参数

   1: [root@DB-Server ~]# date -d now
   2:  
   3: Thu Nov 14 22:52:45 PST 2013
   4:  
   5: [root@DB-Server ~]# date -d 'next monday'
   6:  
   7: Mon Nov 18 00:00:00 PST 2013
   8:  
   9: [root@DB-Server ~]# date -d yesterday +%Y%m%d
  10:  
  11: 20131113
  12:  

4:显示文件中的时间

   1: [root@DB-Server ~]# cat >date.txt
   2:  
   3: 2013-11-17 10:54:00
   4:  
   5: 2013-11-22 11:11:11
   6:  
   7: [root@DB-Server ~]# more date.txt 
   8:  
   9: 2013-11-17 10:54:00
  10:  
  11: 2013-11-22 11:11:11
  12:  
  13: [root@DB-Server ~]# date -f date.txt
  14:  
  15: Sun Nov 17 10:54:00 PST 2013
  16:  
  17: Fri Nov 22 11:11:11 PST 2013
  18:  

5:显示文件最后修改的时间

   1: [root@DB-Server ~]# date -r install.log
   2:  
   3: Fri Sep 6 11:31:15 PDT 2013
   4:  

6:按各种格式显示当前日期时间

这个命令的格式参数实在是太多了,在此没有必要每一个参数都尝试一遍,大家可以对唱Format参数表,自己敲一敲命令实践一下。

   1: 6.1 只显示当前日期的年份
   2:  
   3: [root@DB-Server ~]# date +%Y
   4:  
   5: 2013
   6:  
   7: 6.2 只显示当前日期的月份
   8:  
   9: [root@DB-Server ~]# date +%m
  10:  
  11: 11
  12:  
  13: 6.3 显示各种格式类型的日期
  14:  
  15: [root@DB-Server ~]# date +%D
  16:  
  17: 11/14/13
  18:  
  19: [root@DB-Server ~]# date '+%Y-%m-%d'
  20:  
  21: 2013-11-14
  22:  
  23: [root@DB-Server ~]# date +%Y-%m-%d
  24:  
  25: 2013-11-14
  26:  
  27: [root@DB-Server ~]# date +%m/%d/%y
  28:  
  29: 11/14/13
  30:  
  31: [root@DB-Server ~]# date +%m/%d/%Y
  32:  
  33: 11/14/2013
  34:  
  35: .......
  36:  

7: 设置系统时间

   1: [root@DB-Server ~]# date -s "2013-11-14 00:00:00"
   2:  
   3: Thu Nov 14 00:00:00 PST 2013
作者:潇湘隐者
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.

Linux命令学习总结:date命令【转】的更多相关文章

  1. Linux命令学习-date

    date命令可以用来显示和修改系统日期时间,注意不是time命令. 1.在命令行输入date显示当前时间 [root@vm4 logs]# dateSat Nov 22 00:00:02 CST 20 ...

  2. linux修改系统时间date命令加clock -w

    http://m.jb51.net/LINUXjishu/117784.html 修改linux系统时间的方法(date命令) 11-18 23:22:27作者:脚本之家 命令格式为: date -s ...

  3. linux shell 学习笔记--内部命令学习

    .基本命令 新手必须要掌握的初级命令 ls 基本的列出所有文件的命令.但是往往就是因为这个命令太简单,所以我们总是低估它.比如 ,用 -R 选项,这是递归选项,ls 将会以目录树的形式列出所有文件, ...

  4. Linux命令学习神器!命令看不懂直接给你解释!

    大家都知道,Linux 系统有非常多的命令,而且每个命令又有非常多的用法,想要全部记住所有命令的所有用法,恐怕是一件不可能完成的任务. 一般情况下,我们学习一个命令时,要么直接百度去搜索它的用法,要么 ...

  5. linux命令总结之date命令

    命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the system date and time 指令所在路径:/bin/date 命令语法: date [OP ...

  6. Linux命令学习笔记- vmstat命令实战详解

    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...

  7. LINUX基础学习之基础命令(3)--2019-11-22

    1.命令行展开   ~ USERNAME:展开用户的主目录 [root@CentOS-7-43 ~]# ls ~roo 公共 模板 视频 图片 文档 下载 音乐 桌面 {}:可承载一个以逗号分隔的列表 ...

  8. LINUX基础学习之基础命令(2)--2019-11-18

    1.ls  :命令(list):列出指定目录下的内容 用法:ls [选项] [文件名...] [root@Redhat-7-43 ~]# which ls alias ls='ls --color=a ...

  9. Linux基础学习之基础命令(1)--2019-11-14

    查看命令路径其他方法: which 命令: which [options] [--] programname [...] -a:显示所有匹配的程序文件,而非第一个: --skip-alias:略过别名 ...

  10. Linux命令学习总结: file命令

    命令简介: 该命令用来识别文件类型,也可用来辨别一些文件的编码格式.它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的. 执行权限 :All User 指令所在 ...

随机推荐

  1. 【洛谷5月月赛】玩游戏(NTT,生成函数)

    [洛谷5月月赛]玩游戏(NTT,生成函数) 题面 Luogu 题解 看一下要求的是什么东西 \((a_x+b_y)^i\)的期望.期望显然是所有答案和的平均数. 所以求出所有的答案就在乘一个逆元就好了 ...

  2. 在Android中通过导入静态数据库来提高应用第一次的启动速度

    一个Android应用给用户的第一印象非常重要,除了要有好的创意和美观的界面,性能也是很关键的部分,本文讨论的就是第一次启动的速度问题. Android应用的启动过程不能让用户等待太长时间,个人觉得最 ...

  3. intest

    /* ============================================================================ Name : http.c Author ...

  4. centos7 修改 PATH环境变量(注意,不是添加!!!TMD)

    起因都是,参照阿里云的Java环境配置,MMP~ 现在我们分析一下这几句话.JAVA_HOME和JRE_HOME都是没问题的 CLASSPATH:注意 [  lib$:JRE  ]这部分,Linux环 ...

  5. 在Vue中如何使用axios跨域访问数据(转)

    最近在项目中需要用到axios,所以就恶补一下这个axios到底是什么东东.越来它是vue-resource的替代品,官网也说了,以后都用axios, vue-resource不在维护.那么这个axi ...

  6. 洛谷P2148 [SDOI2009]E&D(博弈论)

    洛谷题目传送门 先安利蒟蒻仍在施工的博弈论总结 首先根据题目,石子被两两分组了,于是根据SG定理,我们只要求出每一组的SG值再全部异或起来就好啦. 把每一对数看成一个ICG,首先,我们尝试构造游戏的状 ...

  7. 枚举 enum 成员变量初始化

    typedef enum { A1, A2, A3, A4 = , A_END }A; 如果A1赋值为5,则下列依次递增1,即A2等于6,A3等于7: 由于A4赋值为10,所以A_END等于11 如果 ...

  8. Mysql占用CPU过高如何优化,如何解决

    2017-02-28 15:13 331人阅读 评论(0) 收藏 举报   MySQL占用CPU过高如何优化 一次生产DB服务器的 超负荷运行问题解决: 1.查看生产DB服务器top列表, 执行 to ...

  9. 理解 PHP 依赖注入

    Laravel框架的依赖注入确实很强大,并且通过容器实现依赖注入可以有选择性的加载需要的服务,减少初始化框架的开销,下面是我在网上看到的一个帖子,写的很好拿来与大家分享,文章从开始按照传统的类设计数据 ...

  10. elasticsearch索引清理

    只是记录两条简单的命令 查看所有的索引文件: curl -XGET http://localhost:9200/_cat/indices?v 删除索引文件以释放空间: curl -XDELETE ht ...