linux-date简介

1 格式:date

显示当前日期时间

eg:

[root@master shelldir]# date 
Tue Dec 30 05:28:57 PST 2014

2 格式:date +FORMAT

根据指定格式显示当前时间。

常用FORMAT:

%Y  YYYY格式的年份(Year)  
[root@master shelldir]# date +%Y
2014


%m  mm格式的月份(),01-12
[root@master shelldir]# date +%m
12


%d   dd格式的日期(day of month),01-31
[root@master shelldir]# date +%d
30


%H   HH格式的小时数(),00-23
[root@master shelldir]# date +%H
05


%M  MM格式的分钟数(),00-59
[root@master shelldir]# date +%M
34


%S   SS格式的秒数(),00-59
[root@master shelldir]# date +%S
02


%F   YYYY-mm-dd格式的完整日期(Full date),同%Y-%m-%d
[root@master shelldir]# date +%F
2014-12-30


%T   HH-MM-SS格式的时间(Time),同%H:%M:%S
[root@master shelldir]# date +%T
05:35:41


%s   自1970年以来的秒数。C函数time(&t) 或者Java中 System.currentTimeMillis()/1000, new Date().getTime()/1000
[root@master shelldir]# date +%s
1419946589

%w   星期几,0-6,0表示星期天
[root@master shelldir]# date +%w
2

%u   星期几,1-7,7表示星期天
[root@master shelldir]# date +%u
2


注意以上格式是可以任意组合的,还可以包括非格式串,比如 date "+今天是%Y-%d-%m,现在是$H:%M:%S"
[root@master shelldir]# date "+today is %Y-%m-%d %H:%M:%S"
today is 2014-12-30 05:42:59

更多格式 man date 或 info date

3 格式:

date -d STRING   

date -d STRING +FORMAT

显示用STRING指定的日期时间(display time described by STRING, not ‘now’)

格式:date -s STRING

设置当前时间为STRING指定的日期时间

STRING可谓变化多样,支持很多种日期时间的描述方式,常用的日期表示方式简单列举如下:

今天:
[root@master ~]# date -d today +%F-%T
2014-12-30-06:14:15

或者:
[root@master ~]# date -d now +%F-%T
2014-12-30-06:16:26

明天:


date -d tomorrow

date -d next-day

date -d next-days

date -d "next day"    貌似只要中间没有间隔符,那么外面需要用""套住

date -d "next days"   days 和 day这里表达了同一个效果

date -d "+1 day"

date -d "+1 days"

date -d "1 day"

date -d "1 days"

date -d "-1 day ago"    -1 ago也表示负  负负得正 因此表示明天

date -d "-1 days ago"

[root@master ~]# date -d "-1 day ago"
Wed Dec 31 06:32:40 PST 2014


昨天:
date -d yesterday

date -d last-day

date -d last-days

date -d "last day"

date -d "last days"

date -d "-1 day"

date -d "-1 days"

date -d "1 day ago"

date -d "1 days ago"

[root@master ~]# date -d "1 days ago"
Mon Dec 29 06:48:39 PST 2014


前天:
date -d "2 day ago"

date -d "2 days ago"

date -d "-2 day"

date -d "-2 days"

[root@master ~]# date -d "-2 days" +%F:%T
2014-12-28:06:50:31


  
大前天:

date -d "3 day ago"

date -d "3 days ago"

date -d "-3 day"

date -d "-3 days"


上周,一周前:

date -d "1 week ago"

date -d "1 weeks ago"

上个星期五(不是上周五):

date -d "last-friday"

date -d "last friday"

上月,一月前:

date -d last-month

date -d last-months

date -d "-1 month"

date -d "-1 months"

下月,一月后:

date -d next-month

date -d next-months

date -d "+1 month"

date -d "+1 months"

去年,一年前:

date -d last-year

date -d last-years

date -d "-1 year"

date -d "-1 years"

明年,一年后:

date -d next-year

date -d next-years

date -d "+1 year"

date -d "+1 years"

一小时前:

date -d "last-hour"

date -d "last-hours"

date -d "1 hour ago"

date -d "1 hours ago"

一小时后:

date -d "1 hour"

date -d "1 hours"

一分钟前:

date -d "1 minute ago"

date -d "1 minutes ago"

一分钟后:

date -d "1 minute"

date -d "1 minutes"

一秒前:

date -d "1 second ago"

date -d "1 seconds ago"

一秒后:

date -d "1 second"

date -d "1 seconds"

待续。。。。。

猜你喜欢

转载自chengjianxiaoxue.iteye.com/blog/2171328