linux命令总结之date命令

命令简介:

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 [[email protected] scripts]# date --help
 2 Usage: date [OPTION]... [+FORMAT]
 3   or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
 4 Display the current time in the given FORMAT, or set the system date.
 5
 6   -d, --date=STRING         display time described by STRING, not `now‘
 7   -f, --file=DATEFILE       like --date once for each line of DATEFILE
 8   -r, --reference=FILE      display the last modification time of FILE
 9   -R, --rfc-2822            output date and time in RFC 2822 format.
10                             Example: Mon, 07 Aug 2006 12:34:56 -0600
11       --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
12                             TIMESPEC=`date‘, `seconds‘, or `ns‘ for
13                             date and time to the indicated precision.
14                             Date and time components are separated by
15                             a single space: 2006-08-07 12:34:56-06:00
16   -s, --set=STRING          set time described by STRING
17   -u, --utc, --universal    print or set Coordinated Universal Time
18       --help     display this help and exit
19       --version  output version information and exit
20
21 FORMAT controls the output.  Interpreted sequences are:
22
23   %%   a literal %
24   %a   locale‘s abbreviated weekday name (e.g., Sun)
25   %A   locale‘s full weekday name (e.g., Sunday)
26   %b   locale‘s abbreviated month name (e.g., Jan)
27   %B   locale‘s full month name (e.g., January)
28   %c   locale‘s date and time (e.g., Thu Mar  3 23:05:25 2005)
29   %C   century; like %Y, except omit last two digits (e.g., 20)
30   %d   day of month (e.g, 01)
31   %D   date; same as %m/%d/%y
32   %e   day of month, space padded; same as %_d
33   %F   full date; same as %Y-%m-%d
34   %g   last two digits of year of ISO week number (see %G)
35   %G   year of ISO week number (see %V); normally useful only with %V
36   %h   same as %b
37   %H   hour (00..23)
38   %I   hour (01..12)
39   %j   day of year (001..366)
40   %k   hour ( 0..23)
41   %l   hour ( 1..12)
42   %m   month (01..12)
43   %M   minute (00..59)
44   %n   a newline
45   %N   nanoseconds (000000000..999999999)
46   %p   locale‘s equivalent of either AM or PM; blank if not known
47   %P   like %p, but lower case
48   %r   locale‘s 12-hour clock time (e.g., 11:11:04 PM)
49   %R   24-hour hour and minute; same as %H:%M
50   %s   seconds since 1970-01-01 00:00:00 UTC
51   %S   second (00..60)
52   %t   a tab
53   %T   time; same as %H:%M:%S
54   %u   day of week (1..7); 1 is Monday
55   %U   week number of year, with Sunday as first day of week (00..53)
56   %V   ISO week number, with Monday as first day of week (01..53)
57   %w   day of week (0..6); 0 is Sunday
58   %W   week number of year, with Monday as first day of week (00..53)
59   %x   locale‘s date representation (e.g., 12/31/99)
60   %X   locale‘s time representation (e.g., 23:13:48)
61   %y   last two digits of year (00..99)
62   %Y   year
63   %z   +hhmm numeric timezone (e.g., -0400)
64   %:z  +hh:mm numeric timezone (e.g., -04:00)
65   %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
66   %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
67   %Z   alphabetic time zone abbreviation (e.g., EDT)
68
69 By default, date pads numeric fields with zeroes.
70 The following optional flags may follow `%‘:
71
72   -  (hyphen) do not pad the field
73   _  (underscore) pad with spaces
74   0  (zero) pad with zeros
75   ^  use upper case if possible
76   #  use opposite case if possible
77
78 After any flags comes an optional field width, as a decimal number;
79 then an optional modifier, which is either
80 E to use the locale‘s alternate representations if available, or
81 O to use the locale‘s alternate numeric symbols if available.
82
83 Report date bugs to bug-[email protected]
84 GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
85 General help using GNU software: <http://www.gnu.org/gethelp/>
86 For complete documentation, run: info coreutils ‘date invocation‘

date --help

  1 [[email protected] scripts]# man date
  2 DATE(1)                          User Commands                         DATE(1)
  3
  4 NAME
  5        date - print or set the system date and time
  6
  7 SYNOPSIS
  8        date [OPTION]... [+FORMAT]
  9        date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
 10
 11 DESCRIPTION
 12        Display  the current time in the given FORMAT, or set the system
 13 :
 14 DATE(1)                          User Commands                         DATE(1)
 15
 16 NAME
 17        date - print or set the system date and time
 18
 19 SYNOPSIS
 20        date [OPTION]... [+FORMAT]
 21        date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
 22
 23 DESCRIPTION
 24        Display  the current time in the given FORMAT, or set the system
 25        date.
 26
 27        -d, --date=STRING
 28               display time described by STRING, not ‘now’
 29
 30        -f, --file=DATEFILE
 31               like --date once for each line of DATEFILE
 32
 33        -r, --reference=FILE
 34               display the last modification time of FILE
 35
 36        -R, --rfc-2822
 37               output date and time in RFC 2822 format.   Example:  Mon,
 38               07 Aug 2006 12:34:56 -0600
 39
 40        --rfc-3339=TIMESPEC
 41               output   date   and  time  in  RFC  3339  format.   TIME-
 42               SPEC=‘date’, ‘seconds’, or ‘ns’ for date and time to  the
 43               indicated  precision.  Date and time components are sepa-
 44               rated by a single space: 2006-08-07 12:34:56-06:00
 45
 46        -s, --set=STRING
 47               set time described by STRING
 48
 49        -u, --utc, --universal
 50               print or set Coordinated Universal Time
 51
 52        --help display this help and exit
 53
 54        --version
 55               output version information and exit
 56
 57        FORMAT controls the output.  Interpreted sequences are:
 58
 59        %%     a literal %
 60
 61        %a     locale’s abbreviated weekday name (e.g., Sun)
 62
 63        %A     locale’s full weekday name (e.g., Sunday)
 64
 65        %b     locale’s abbreviated month name (e.g., Jan)
 66
 67        %B     locale’s full month name (e.g., January)
 68
 69        %c     locale’s date and time (e.g., Thu Mar  3 23:05:25 2005)
 70
 71        %C     century; like %Y, except omit last two digits (e.g., 20)
 72
 73        %d     day of month (e.g, 01)
 74
 75        %D     date; same as %m/%d/%y
 76
 77        %e     day of month, space padded; same as %_d
 78
 79        %F     full date; same as %Y-%m-%d
 80
 81        %g     last two digits of year of ISO week number (see %G)
 82
 83        %G     year of ISO week number (see %V);  normally  useful  only
 84               with %V
 85
 86        %h     same as %b
 87
 88        %H     hour (00..23)
 89
 90        %I     hour (01..12)
 91
 92        %j     day of year (001..366)
 93
 94        %k     hour ( 0..23)
 95
 96        %l     hour ( 1..12)
 97
 98        %m     month (01..12)
 99
100        %M     minute (00..59)
101
102        %n     a newline
103
104        %N     nanoseconds (000000000..999999999)
105
106        %p     locale’s  equivalent  of  either  AM  or PM; blank if not
107               known
108
109        %P     like %p, but lower case
110
111        %r     locale’s 12-hour clock time (e.g., 11:11:04 PM)
112
113        %R     24-hour hour and minute; same as %H:%M
114
115        %s     seconds since 1970-01-01 00:00:00 UTC
116
117        %S     second (00..60)
118
119        %t     a tab
120
121        %T     time; same as %H:%M:%S
122
123        %u     day of week (1..7); 1 is Monday
124
125        %U     week number of year, with Sunday as  first  day  of  week
126               (00..53)
127
128        %V     ISO  week  number,  with  Monday  as  first  day  of week
129               (01..53)
130
131        %w     day of week (0..6); 0 is Sunday
132
133        %W     week number of year, with Monday as  first  day  of  week
134               (00..53)
135
136        %x     locale’s date representation (e.g., 12/31/99)
137
138        %X     locale’s time representation (e.g., 23:13:48)
139
140        %y     last two digits of year (00..99)
141
142        %Y     year
143
144        %z     +hhmm numeric timezone (e.g., -0400)
145
146        %:z    +hh:mm numeric timezone (e.g., -04:00)
147
148        %::z   +hh:mm:ss numeric time zone (e.g., -04:00:00)
149
150        %:::z  numeric  time  zone  with : to necessary precision (e.g.,
151               -04, +05:30)
152
153        %Z     alphabetic time zone abbreviation (e.g., EDT)
154
155        By default, date pads numeric fields with zeroes.  The following
156        optional flags may follow ‘%’:
157
158        -      (hyphen) do not pad the field
159
160        _      (underscore) pad with spaces
161
162        0      (zero) pad with zeros
163
164        ^      use upper case if possible
165
166        #      use opposite case if possible
167
168        After any flags comes an optional field width, as a decimal num-
169        ber; then an optional modifier, which is either  E  to  use  the
170        locale’s alternate representations if available, or O to use the
171        locale’s alternate numeric symbols if available.
172
173 DATE STRING
174        The --date=STRING is a mostly free format  human  readable  date
175        string  such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29
176        16:21:42" or even "next Thursday".  A date  string  may  contain
177        items  indicating  calendar date, time of day, time zone, day of
178        week, relative time,  relative  date,  and  numbers.   An  empty
179        string indicates the beginning of the day.  The date string for-
180        mat is more complex than is easily documented here but is  fully
181        described in the info documentation.
182
183 ENVIRONMENT
184        TZ     Specifies the timezone, unless overridden by command line
185               parameters.  If neither is specified,  the  setting  from
186               /etc/localtime is used.
187
188 AUTHOR
189        Written by David MacKenzie.
190
191 REPORTING BUGS
192        Report date bugs to bug-[email protected]
193        GNU   coreutils  home  page:  <http://www.gnu.org/software/core-
194        utils/>
195        General help using GNU software: <http://www.gnu.org/gethelp/>
196        Report  date   translation   bugs   to   <http://translationpro-
197        ject.org/team/>
198
199 COPYRIGHT
200        Copyright ? 2010 Free Software Foundation, Inc.  License GPLv3+:
201        GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
202        This is free software: you are free to change  and  redistribute
203        it.  There is NO WARRANTY, to the extent permitted by law.
204
205 SEE ALSO
206        The  full documentation for date is maintained as a Texinfo man-
207        ual.  If the info and date programs are  properly  installed  at
208        your site, the command
209
210               info coreutils ‘date invocation‘
211
212        should give you access to the complete manual.
213
214 GNU coreutils 8.4                October 2014                          DATE(1)
215 (END) 

man date

2:运用-d参数

[[email protected] scripts]# date
Sun Jan 29 10:46:03 CST 2017
[[email protected] scripts]# date -d now
Sun Jan 29 10:46:07 CST 2017
[[email protected] scripts]# date -d ‘next monday‘
Mon Jan 30 00:00:00 CST 2017
[[email protected] scripts]# date -d yesterday +%Y%m%d
20170128

3:显示文件中的时间

[[email protected] scripts]# cat >date.txt
2013-11-17 10:54:00
2013-11-22 11:11:11

[[email protected] scripts]# more date.txt
2013-11-17 10:54:00
2013-11-22 11:11:11

[[email protected] scripts]# date -f date.txt
Sun Nov 17 10:54:00 CST 2013
Fri Nov 22 11:11:11 CST 2013

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

[[email protected] scripts]# date -r date.txt
Sun Jan 29 10:49:11 CST 2017

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

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

[[email protected] scripts]# date +%Y
2017
[[email protected] scripts]# date +%m
01
[[email protected] scripts]# date +%D
01/29/17
[[email protected] scripts]# date ‘+%Y-%m-%d‘
2017-01-29
[[email protected] scripts]# date +%Y-%m-%d
2017-01-29
[[email protected] scripts]# date +%m/%d/%y
01/29/17

6: 设置系统时间

[[email protected] scripts]# date -s "2016-11-11 00:00:00"
Fri Nov 11 00:00:00 CST 2016
[[email protected] scripts]# date
Fri Nov 11 00:00:05 CST 2016

7:请给出如下格式的date命令,如:11-02-26。再给出实现按周输出,如:周六为6,请分别给出命令

[[email protected] scripts]# date +%y-%m-%d
17-01-29
[[email protected] scripts]# date +%F
2017-01-29
[[email protected] scripts]# date "+%Y-%m-%d %H:%M:%S"
2017-01-29 10:57:02

生产场景中常用时间格式来打包数据:

[[email protected] andy]# tar zcvf etc-$(date +%F).tar.gz /etc
[[email protected] andy]# tar zcvf etc-`date +%F`.tar.gz /etc

解析命令方法:反引号,或$(),如上

[[email protected] andy]# date +%w  ##显示周,0-6
3
显示过去与未来时间:
[[email protected] andy]# date +%F
2016-11-16
[[email protected] andy]# date +%F -d "-1day"
2016-11-15
[[email protected] andy]# date +%F -d "+2day"
2016-11-18
[[email protected] andy]# date +%F -d "+24hour"
2016-11-17
[[email protected] andy]# date +%F-%H -d "+2hour"
2016-11-16-14
时间: 2024-10-14 19:21:49

linux命令总结之date命令的相关文章

shell脚本删除N天前的文件夹-----附linux和mac上date命令的不同

背景: 每日构建的东西,按日期放到不同的文件夹里.如今天的构建放到2015-06-01里,明天的就放到2015-06-02里,依次类推.时间久了,需要一个脚本删除N天前的文件夹.(本例中N=7,即删除一周前的构建). 下面直接上代码,linux版: #! /bin/bash historyDir=~/test/ today=$(date +%Y-%m-%d) echo "---------today is $today-----------" tt=`date -d last-week

linux (2) Date 命令

功能说明:显示或设置系统时间与日期. 语 法:date [-d <字符串>] [-u] [+%H%I%K%l%M%P%r%s%S%T%X%Z%a%A%b%B%c%d%D%j%m%U%w%x%y%Y%n%t] 或 date [-s <字符串>][-u] [MMDDhhmmCCYYss] 或 date [--help][--version] 补充说明:第一种语法可用来显示系统日期或时间,以%为开头的参数为格式参数,可指定日期或时间的显示格式.第二种语法可用来设置系统日期 与时间.只有管

Linux date命令

date命令是Linux中常用的一个命令,主要作用就是显示本机当前时间.如下: 在本人Linux操作系统中,date命令默认实现格式为年月日-星期-时分秒,然后是CST中央标准时间.不过,你也可以自行修改显示格式,如: 其中: 1.年份:+%Y表示以四位数字格式打印年份,而+%y表示以两位数字格式打印年份: 2.月份:+%m表示月份: 3.日期:+%d表示日期(天): 4.时:+%H表示小时: 5.分:+%M表示分钟: 6.秒:+%S表示秒: 7.周:+%w表示星期,1-6代表周一至周六,0代表

Linux——date命令,目录相关的命令

一.日期命令 1.date命令 命令格式: date [OPTION]... [+FORMAT] # 显示当前系统时间: 显示时间格式(date '+%y,%m,%d,%H,%M,%S'): format格式 含义 %Y,%y 年 %m 月 %d 日 %H 时 %M 分 %S 秒 %u 周几 date [MMDDhhmm[[CC]YY][.ss]]  # 设置系统时间 MM: 月分 DD:几号 hh: 小时 mm: 分钟 YY: 两位年份 CCYY: 四位年份 .ss: 秒钟 Linux的两种时

总结四:Linux与时间相关的常用命令

总结四:Linux与时间相关的常用命令 1.         date命令  --显示时间或设定时间 用法(1):date [options]... [+format]    显示 +%F,+%D  显示日期 +%T    显示时间 +%Y  年 +%m    月 +%d     日 +%H    时 +%M    分 +%S     秒 +%u     星期 +%s     时间戳计时法,从Unix元年(1970-01-01 00:00:00) 到此刻所经过的秒数 [[email protec

linux date 命令使用

http://blog.sina.com.cn/s/blog_654c6ec70101mx99.html http://codingstandards.iteye.com/blog/1157513 linux date 命令使用,码迷,mamicode.com

【转】每天一个linux命令(37):date命令

原文网址:http://www.cnblogs.com/peida/archive/2012/12/13/2815687.html 在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数]... [+格式] 2.命令功能: date 可以用来显示或设定系统的日期与时间. 3.命令参数: 必要参数: %H 小时(以00-23来表示). %I 小时(以01

Linux 的date命令用法

Linux date命令的用法 在linux shell编程中,经常用到日期的加减运算 以前都是自己通过expr函数计算,很麻烦 其实date命令本身提供了日期的加减运算 非常方便.例如:得到昨天的时间date +%Y%m%d --date="-1 day" date 用法: date [OPTION]... [+FORMAT]date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]date 可以用来显示或设定系统的日期与时间.1.在显示方

linux命令--date命令&cal命令

date命令是用来显示时间的,cal命令是用来显示日历的. 其中date命令的一些常用格式有 %H 小时(以00-23来表示).  %I 小时(以01-12来表示).  %K 小时(以0-23来表示).  %l 小时(以0-12来表示).  %M 分钟(以00-59来表示).  %P AM或PM.  %r 时间(含时分秒,小时以12小时AM/PM来表示).  %s 总秒数.起算时间为1970-01-01 00:00:00 UTC.  %S 秒(以本地的惯用法来表示).  %T 时间(含时分秒,小