shell date 相关使用

#格式化输出                                                                                
$> date +"%Y-%m-%d"                                                                        
2009-12-07                                                                                 
                                                                                           
#输出昨天日期                                                                              
$> date -d "1 day ago" +"%Y-%m-%d"                                                         
2012-11-19                                                                                 
                                                                                           
#2秒后输出                                                                                 
$> date -d "2 second" +"%Y-%m-%d %H:%M.%S"                                                 
2012-11-20 14:21.31                                                                        
                                                                                           
#传说中的 1234567890 秒                                                                    
$> date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%m:%S"                            
2009-02-13 23:02:30                                                                        
                                                                                           
#普通转格式                                                                                
$> date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"                                               
2009/12/12 00:00.00                                                                        
                                                                                           
#apache格式转换                                                                            
$> date -d "Dec 5, 2009 12:00:37 AM" +"%Y-%m-%d %H:%M.%S"                                  
2009-12-05 00:00.37                                                                        
                                                                                           
#格式转换后时间游走                                                                        
$>date -d "Dec 5, 2009 12:00:37 AM 2 year ago" +"%Y-%m-%d %H:%M.%S"                        
2007-12-05 00:00.37                                                                        
                                                                                           
#加减操作                                                                                  
date +%Y%m%d                   //显示前天年月日                                            
date -d "+1 day" +%Y%m%d       //显示前一天的日期                                          
date -d "-1 day" +%Y%m%d       //显示后一天的日期                                          
date -d "-1 month" +%Y%m%d     //显示上一月的日期                                          
date -d "+1 month" +%Y%m%d     //显示下一月的日期                                          
date -d "-1 year" +%Y%m%d      //显示前一年的日期                                          
date -d "+1 year" +%Y%m%d      //显示下一年的日期                                          
                                                                                           
                                                                                           
#设定时间                                                                                  
date -s                        //设置当前时间,只有root权限才能设置,其他只能查看          
date -s 20120523               //设置成20120523,这样会把具体时间设置成空00:00:00          
date -s 01:01:01               //设置具体时间,不会对日期做更改                            
date -s "01:01:01 2012-05-23"  //这样可以设置全部时间                                      
date -s "01:01:01 20120523"    //这样可以设置全部时间                                      
date -s "2012-05-23 01:01:01"  //这样可以设置全部时间                                      
date -s "20120523 01:01:01"    //这样可以设置全部时间                                      
                                                                                           
有时需要检查一组命令花费的时间,举例:                                                     
                                                                                           
#!/bin/bash                                                                                
                                                                                           
start=$(date +%s)                                                                          
nmap www.1987.name &> /dev/null                                                            
                                                                                           
end=$(date +%s)                                                                            
difference=$(( end - start ))                                                              
echo $difference seconds.                                                                  

posted on 2014-04-10 23:12  mybloges  阅读(369)  评论(0编辑  收藏  举报

导航