When invoked without arguments, the date command displays the current date and time. Depending on the options specified, date will set the date and time or print it in a user defined way. I’ve seen many people writing a perl script for calculating yesterday or tomorrow. Computer loves numbers but we love relative terms like 2 days ago. Luckily GNU date command is designed to handle relative date calculation.
Why use relative date formats?
[a] Ease of use
[b] To write your own scripts
[c] Automate task using cron (example run a job on last day of the month or Nth day of the month or 3rd Friday and so on)
First print today’s date:
$ date
Sun Jun 17 12:17:24 CDT 2007
Now display Yesterday’s date:
$ date --date="1 days ago"
OR try:
$ date --date="yesterday"
Sat Jun 16 12:17:20 CDT 2007
Now display Tomorrow’s date:
$ date --date="-1 days ago"
Or better try:
$ date --date="next day"
Sat Jun 16 12:17:20 CDT 2007
Getting date in the future
To get tomorrow and day after tomorrow (tomorrow+N) use day word to get date in the future:
Getting date in the past
To get yesterday and earlier day in the past use string day ago:
Moving by whole years or months
You can add year and months keywords to get more accurate date:
$ date --date='2 year ago' # past
$ date --date='3 years' # go into future
$ date --date='2 days' # future
$ date --date='1 month ago' # past
$ date --date='2 months' # future
Moving date using more precise units
- You can use fortnight for 14 day
- Week for 7 days
- hour for 60 minutes
- minute for 60 seconds
- second for one second
- You can also use this / now / today keywords to stress the meaning
To print the date of this Friday:
$ date --date='this Friday'
To print the date of the day six months and 15 day
$ date --date='6 months 15 day'
To print the date of the day two months and 5 days ago:
$ date --date='2 months 5 day ago'
You can also use relative format to setup date and time. For example to set the system clock forward by 30 minutes, enter:
# date --set='+30 minutes'
To display date in epoch time:
$ date --date='1970-01-01 00:00:01 UTC +5 hours' +%s
http://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html
Linux date 命令使用技巧
date命令
date命令的功能是显示和设置系统日期和时间。
该命令的一般格式为: date [选项] 显示时间格式(以+开头,后面接格式)
date 设置时间格式
命令中各选项的含义分别为:
-d datestr, –date datestr 显示由datestr描述的日期
-s datestr, –set datestr 设置datestr 描述的日期
-u, –universal 显示或设置通用时间
时间域
% H 小时(00..23)
% I 小时(01..12)
% k 小时(0..23)
% l 小时(1..12)
% M 分(00..59)
% p 显示出AM或PM
% r 时间(hh:mm:ss AM或PM),12小时
% s 从1970年1月1日00:00:00到目前经历的秒数
% S 秒(00..59)
% T 时间(24小时制)(hh:mm:ss)
% X 显示时间的格式(%H:%M:%S)
% Z 时区 日期域
% a 星期几的简称( Sun..Sat)
% A 星期几的全称( Sunday..Saturday)
% b 月的简称(Jan..Dec)
% B 月的全称(January..December)
% c 日期和时间( Mon Nov 8 14:12:46 CST 1999)
% d 一个月的第几天(01..31)
% D 日期(mm/dd/yy)
% h 和%b选项相同
% j 一年的第几天(001..366)
% m 月(01..12)
% w 一个星期的第几天(0代表星期天)
% W 一年的第几个星期(00..53,星期一为第一天)
% x 显示日期的格式(mm/dd/yy)
% y 年的最后两个数字( 1999则是99)
% Y 年(例如:1970,1996等)
需要特别说明的是,只有超级用户才能用date命令设置时间,一般用户只能用date命令显示时间。
例1:用指定的格式显示时间。
$ date ‘+This date now is =>%x ,time is now =>%X ,thank you !’
This date now is =>11/12/99 ,time is now =>17:53:01 ,thank you !
例2:用预定的格式显示当前的时间。
# date
Fri Nov 26 15:20:18 CST 1999
例3:设置时间为下午14点36分。
# date -s 14:36:00
Fri Nov 26 14:15:00 CST 1999
例4:设置时间为1999年11月28号。
# date -s 991128
Sun Nov 28 00:00:00 CST 1999
例5:设置一天前
date –date “1 days ago” +”%Y-%m-%d”
Date 命令参数小技巧
LastUpdated:2006-03-27
By:Gman!
-for beginner:wink:此参数可以用于日志处理和时间统计;
由于Linux对man date -d 参数说的比较模糊,故举例如下:
# -d, –date=STRING display time described by STRING, not `now’
For Linux
[root@Gman root]# date -d next-day +%Y%m%d
20060328
[root@Gman root]# date -d last-day +%Y%m%d
20060326
[root@Gman root]# date -d yesterday +%Y%m%d
20060326
[root@Gman root]# date -d tomorrow +%Y%m%d
20060328
[root@Gman root]# date -d last-month +%Y%m
200602
[root@Gman root]# date -d next-month +%Y%m
200604
[root@Gman root]# date -d next-year +%Y
2007
——————————————————————————
而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
http://www.zks.cn/article.asp?id=41