介绍:
显示系统时间,根据参数的不同格式不同
用法:
[root@uyhd000225 ~]# man date
DATE(1) User Commands DATE(1)
NAME
date - print or set the system date and time
SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
DESCRIPTION
Display the current time in the given FORMAT, or set the system 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
--rfc-3339=TIMESPEC
output date and time in RFC 3339 format. TIMESPEC=‘date’, ‘seconds’, or ‘ns’ for date and time to the indicated precision.
-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
FORMAT controls the output. The only valid option for the second form specifies Coordinated Universal Time. Interpreted sequences are:
%% a literal %
%a locale’s abbreviated weekday name (e.g., Sun)
%A locale’s full weekday name (e.g., Sunday)
%b locale’s abbreviated month name (e.g., Jan)
%B locale’s full month name (e.g., January)
%c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 21)
%d day of month (e.g, 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour ( 0..23)
%l hour ( 1..12)
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale’s equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale’s 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale’s date representation (e.g., 12/31/99)
%X locale’s time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric timezone (e.g., -0400)
%:z +hh:mm numeric timezone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
By default, date pads numeric fields with zeroes. The following optional flags may follow ‘%’:
- (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
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, or O to use the locale’s alternate numeric symbols if available.实例:
在linux shell编程中,经常用到日期的加减运算
以前都是自己通过expr函数计算,很麻烦
其实date命令本身提供了日期的加减运算
非常方便。例如:得到昨天的时间
date +%Y%m%d --date="-1 day"
[root@uyhd000225 ~]# date +%Y%m%d 20131219 [root@uyhd000225 ~]# date +%Y%m%d --date="+1 year" 20141219 [root@uyhd000225 ~]# date +%Y%m%d --date="-1 day" 20131218
2.在设定时间方面
date -s //设置当前时间,只有root权限才能设置,其他只能查看。
date -s 20080523 //设置成20080523,这样会把具体时间设置成空00:00:00
date -s 01:01:01 //设置具体时间,不会对日期做更改
date -s “01:01:01 2008-05-23″ //这样可以设置全部时间
date -s “01:01:01 20080523″ //这样可以设置全部时间
date -s “2008-05-23 01:01:01″ //这样可以设置全部时间
date -s “20080523 01:01:01″ //这样可以设置全部时间
3.加减
date +%Y%m%d //显示现在天年月日
date +%Y%m%d --date="+1 day" //显示后一天的日期
date +%Y%m%d --date="-1 day" //显示前一天的日期
date +%Y%m%d --date="-1 month" //显示上一月的日期
date +%Y%m%d --date="+1 month" //显示下一月的日期
[root@uyhd000225 ~]# date +%Y%m%d --date="+1 month" 20140119 [root@uyhd000225 ~]#
date +%Y%m%d --date="-1 year" //显示前一年的日期
date +%Y%m%d --date="+1 year" //显示下一年的日期
或者更简单点的 date=`date -d -${t}day '+%Y%m%d'` //为t为前几天
4.格式化输出
[root@uyhd000225 ~]# date +%Y%m%d 20131219 [root@uyhd000225 ~]# date +%Y-%m-%d 2013-12-19 [root@uyhd000225 ~]# date +%h:%m:%s 12月:12:1387429235 [root@uyhd000225 ~]# date +%H:%M:%s 13:01:1387429264 [root@uyhd000225 ~]# date +%H:%M:%S 13:01:08 [root@uyhd000225 ~]# date +%Y-%m-%d %H:%M:%S date: 额外的操作数 “%H:%M:%S” 请尝试执行“date --help”来获取更多信息。 [root@uyhd000225 ~]# date +%Y-%m-%d##%H:%M:%S 2013-12-19##13:01:53 [root@uyhd000225 ~]# date "+%Y-%m-%d %H:%M:%S" 2013-12-19 13:02:08 [root@uyhd000225 ~]# date -d now 2013年 12月 19日 星期四 13:02:44 CST [root@uyhd000225 ~]# date -d today 2013年 12月 19日 星期四 13:02:50 CST [root@uyhd000225 ~]# date -d tomorrow 2013年 12月 20日 星期五 13:03:33 CST [root@uyhd000225 ~]# date -d yesterday 2013年 12月 18日 星期三 13:03:44 CST [root@uyhd000225 ~]# date -d "3 days ago" +%Y%m%d 20131216 [root@uyhd000225 ~]# date -d "3 years ago" +%Y%m%d 20101219 [root@uyhd000225 ~]#
[root@localhost admin]# date -s 20110920 //*修改日期
二 9月 20 00:00:00 CST 2011
[root@localhost admin]# date -s 18:40:20 //*修改时间
二 9月 20 18:40:20 CST 2011
[root@localhost admin]# clock -w //*保存当前设置的时间到硬件
显示日期的指令: date
[root@localhost ~]# date
三 5月 12 03:50:41 CST 2010
CST时区的2010年5月12号 星期三 03:50:41
[admin@localhost ~]$ date +%c
%a:显示缩写的星期数字。
%A:显示星期全名。
%b:显示缩写月份,同%h
%B:显示全写月分
%c:显示日期、星期、时间,全写。2010年05月11日 星期二 18时04分54秒
%C:年份除以100所余的整数。
[admin@localhost ~]$ date +%d //*显示系统当日期
[admin@localhost ~]$ date +%D //*缩写显示当前系统时间,mm/dd/yy
[admin@localhost ~]$ date +%F //*全显当前系统时间,同%Y-%m-%d
[admin@localhost ~]$ date +%g //*2位数字显示当前系统年份的后两位
[admin@localhost ~]$ date +%G //*4位数字显示当前系统年份
[admin@localhost ~]$ date +%H //*系统当前时间的时针,24小时制表示
[admin@localhost ~]$ date +%I //*系统当前时间的时针,12小时制表示
[admin@localhost ~]$ date +%j //*当前日期是一年中的第几天
[admin@localhost ~]$ date +%m //*显示当前系统日期的月份,两位数字表示月分
[admin@localhost ~]$ date +%M //*显示当前系统时间的分钟
关于-d参数的详细用法:
用info date命令显示的有关于-d参数的详细用法摘录如下:
`-d DATESTR'
`--date=DATESTR'
Display the date and time specified in DATESTR instead of the
current date and time. DATESTR can be in almost any common
format. It can contain month names, time zones, `am' and `pm',
`yesterday', etc. For example, `--date="2004-02-27
14:19:13.489392193 +0530"' specifies the instant of time that is
489,392,193 nanoseconds after February 27, 2004 at 2:19:13 PM in a
time zone that is 5 hours and 30 minutes east of UTC. *Note Date
Here are a few examples. Also see the documentation for the `-d'
option in the previous section.
* To print the date of the day before yesterday:
date --date='2 days ago'
* To print the date of the day three months and one day hence:
date --date='3 months 1 day'
* To print the day of year of Christmas in the current year:
date --date='25 Dec' +%j
* To print the current full month name and the day of the month:
date '+%B %d'
But this may not be what you want because for the first nine days
of the month, the `%d' expands to a zero-padded two-digit field,
for example `date -d 1may '+%B %d'' will print `May 01'.
* To print a date without the leading zero for one-digit days of the
month, you can use the (GNU extension) `-' flag to suppress the
padding altogether:
date -d 1may '+%B %-d举例如下:
[root@uyhd000225 ~]# date --date='25 Dec' +%j 359 [root@uyhd000225 ~]# date -d '25 Dec' +%j 359 [root@uyhd000225 ~]# date --date='3 months 1 day' 2014年 03月 20日 星期四 13:15:16 CST [root@uyhd000225 ~]# date --date='3 months 1 day ago' 2014年 03月 18日 星期二 13:15:29 CST [root@uyhd000225 ~]# date --date='3 months ago 1 day ago' 2013年 09月 18日 星期三 13:15:42 CST [root@uyhd000225 ~]# date '+%B %d' 十二月 19 [root@uyhd000225 ~]# date -d "2 days ago" 2013年 12月 17日 星期二 13:17:18 CST [root@uyhd000225 ~]# date -d "2 days" 2013年 12月 21日 星期六 13 [root@uyhd000225 ~]# date -d next-day +%Y%m%d 20131220 [root@uyhd000225 ~]# date -d last-day +%Y%m%d 20131218 [root@uyhd000225 ~]# date -d tomorrow +%Y%m%d 20131220 [root@uyhd000225 ~]# date -d last-month +%Y%m 201311 [root@uyhd000225 ~]# date -d last-month +%Y%m%d 20131119 [root@uyhd000225 ~]# date -d next-month +%Y%m%d 20140119 [root@uyhd000225 ~]# date -d next-year +%Y 2014 [root@uyhd000225 ~]#
而FreeBSD则不同;举例如下:
For FreeBSD
bash-2.05b# date -v -1d +%Y%m%d
20060326
bash-2.05b# date -v -1m +%Y%m%d
20060227
bash-2.05b# date -v -1y +%Y%m%d
20050327
更多用法:more date ;info date
















