linux date 使用

date demo:
实例 :
$
> date
Mon Dec  
7   15 : 43 : 39  CST  2009

# 格式化输出
$ >  date  + " %Y-%m-%d "
2009 - 12 - 07

# 时间游走 当前时间 2009-12-07
# year , month , day ,hour , minute,second  - ago
#昨天输出

$ >  date   - " 1 day ago "   + " %Y-%m-%d "
2009 - 12 - 06

# 2秒后输出
$ >  date   - " 2 second "   + " %Y-%m-%d %H:%M.%S "
2009 - 12 - 07   15 : 50.04

# 传说中的 1234567890 秒
$ > date  - " 1970-01-01  1234567890 seconds "    + " %Y-%m-%d %H:%m:%S "
2009 - 02 - 13   23 : 02 : 30

# 其他格式的转换
date  - " $(echo  " 03 / Nov / 2009   11 : 04 : 28 "  |perl -ne 'print  " $ 2  $ 1  $ 3  $ 4 \ n "  if /(.*?) " / ( .*? ) " /(.*?) (.*)/;')  "   + %Y - %m - %d
2009 - 11 - 03

# 普通 转格式
$ >  date  - " 2009-12-12 "   + " %Y/%m/%d %H:%M.%S "
2009 / 12 / 12   00 : 00.00

# apache 格式转换
$ > date   - " Dec 5, 2009 12:00:37 AM "   + " %Y-%m-%d %H:%M.%S "
2009 - 12 - 05   00 : 00.37

# 格式转换 后时间 游走
$ > date   - " Dec 5, 2009 12:00:37 AM 2 year ago "   + " %Y-%m-%d %H:%M.%S "
2007 - 12 - 05   00 : 00.37


# 时间差
#一小时 3600
#一天 86400 秒

$ > st = $(date   - " Dec 5, 2009 12:00:37 AM 1 day ago 1 hour ago  "   + " %s " )
$
> et = $(date   - " Dec 5, 2009 12:00:37 AM "   + " %s " )
$
> echo  " 时间差$[($et-$st)/86400]天,$[($et-$st)/3600%24]小时 "
时间差1天,1小时
date --help
Usage :  date [OPTION]  [ + FORMAT ]
  or
:   date [ - u |-- utc |-- universal] [MMDDhhmm[[CC]YY][ . ss]]
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

本文转自博客园刘凯毅的博客,原文链接:linux date 使用,如需转载请自行联系原博主。














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值