date命令 (按照指定格式)显示或设置系统的日期和时间

 <!-- 与君共勉!欢迎批评指正。-->

date [OPTION]  [+FORMAT]

  -d, --date=STRING         display time described by STRING, not `now' (显示String描述的日期)

  -f, --file=DATEFILE        like --date once for each line of DATEFILE(同--date 从文件中读取Str描述的每行)
  -r, --reference=FILE      display the last modification time of FILE
  -R, --rfc-2822            output date and time in RFC 2822 format  (以RFC 2822格式输出日期和时间)

        --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.(以RFC 3339格式输出日期和时间)
                            TIMESPEC=`date', `seconds', or `ns' for date and time to the indicated precision.
  -s, --set=STRING          set time described by STRING(设置String描述的日期)
  -u, --utc, --universal    print or set Coordinated Universal Time(显示或设置通用时间)
      --help     display this help and exit
      --version  output version information and exit


(1)date -d的使用:
date -d now  #等价于date 

显示3小时前的时间
date -d "3 hour ago" 
date -d "3-hour ago"

显示3天前的时间
date -d "3 day ago"
date -d "3-day ago"

显示3个月前的时间:
date -d "3 month ago"
date -d "3-month ago"

显示3年前的时间:
date -d "3 year ago"
date -d "3-year ago"

显示上星期五的日期
date -d "last-friday"  #
date -d "last friday"

显示n周前的时间
date -d "last week" 
date -d "last-week"

date -d "1 week ago" 
date -d "1-week ago"

date -d "2 week ago"
date -d "2-week ago"


显示3小时后的时间
date -d "3 hour " 
date -d "3-hour "

显示3天后的时间
date -d "3 day "
date -d "3-day "

显示3个月后的时间:
date -d "3 month "
date -d "3-month "

显示3年后的时间:
date -d "3 year "
date -d "3-year "

显示下星期五的时间
date -d "next-friday" 
date -d "next friday"

显示n周后的时间
date -d "next week" 
date -d "next-week"

date -d "1 week " 
date -d "1-week "

date -d "2 week "
date -d "2-week "

date -d "fortnight" # 显示2周后的时间 

date -d "7/1 3 hour" # 显示7月1日起的第三小时
date -d "7/1 3 day" # 显示7月1日起的第3天
date -d "7/1 3 week" # 显示7月1日起的第三周
date -d "7/1 3 month" # 显示7月1日起的第三月
date -d "7/1 3 year" # 显示7月1日起的第三年

date -d  "7/1 3 year"  +"%Y-%m-%d "  #按照YYYY-MM-DD格式显示7月1日起的第三年   结果:2017-07-01

(2)date -s 的使用

date -s 14:20:11          #把时间设置为下午14点20分11秒

date -s  20151228      #把日期设置为2015年12月28号

date -s "1 day ago"     #把日期设置为1天前

date -s "2 day ago"  +"%Y-%m-%d"   #把日期按照YYYY-MM-DD的格式设置为2天前

(3)将时间转化为utc时间格式

   date -d "Mon Mar 10 09:17:32 CST 2014" +%s

   结果:1394414252

   (4) 将utc时间转化为转化为易理解的时间

   date -d "1970-01-01 UTC 1394414252 sec "

  结果:Mon Mar 10 09:17:32 CST 2014

   date -d "1970-01-01 UTC 1394414252 sec " +"%Y-%m-%d %H:%M:%S"
   结果:2014-03-10 09:17:32

猜你喜欢

转载自hq-1900.iteye.com/blog/2028682