zoukankan      html  css  js  c++  java
  • date 命令详解

    date命令是显示或设置系统时间与日期。

    date日期可以以多种格式去打印,也可以使用命令设置固定的格式.

    语法选项

    -d<字符串>:显示字符串所指的日期与时间。字符串前后必须加上双引号; 
    -s<字符串>:根据字符串来设置日期与时间。字符串前后必须加上双引号; 
    -u:显示GMT; 
    --help:在线帮助; 
    --version:显示版本信息。

    参数

    <+时间日期格式>:指定显示时使用的日期时间格式。

    日期格式字符串列表

    %H 小时(以00-23来表示)。 
    %I 小时(以01-12来表示)。 
    %K 小时(以0-23来表示)。 
    %l 小时(以0-12来表示)。 
    %M 分钟(以00-59来表示)。 
    %P AM或PM。 
    %r 时间(含时分秒,小时以12小时AM/PM来表示)。 
    %s 总秒数。起算时间为1970-01-01 00:00:00 UTC。 
    %S 秒(以本地的惯用法来表示)。 
    %T 时间(含时分秒,小时以24小时制来表示)。 
    %X 时间(以本地的惯用法来表示)。 
    %Z 市区。 
    %a 星期的缩写。 
    %A 星期的完整名称。 
    %b 月份英文名的缩写。 
    %B 月份的完整英文名称。 
    %c 日期与时间。只输入date指令也会显示同样的结果。 
    %d 日期(以01-31来表示)。 
    %D 日期(含年月日)。 
    %j 该年中的第几天。 
    %m 月份(以01-12来表示)。 
    %U 该年中的周数。 
    %w 该周的天数,0代表周日,1代表周一,异词类推。 
    %x 日期(以本地的惯用法来表示)。 
    %y 年份(以00-99来表示)。 
    %Y 年份(以四位数来表示)。 
    %n 在显示时,插入新的一行。 
    %t 在显示时,插入tab。 
    MM 月份(必要) 
    DD 日期(必要) 
    hh 小时(必要) 
    mm 分钟(必要)
    ss 秒(选择性)

    使用说明

    格式设定为一个加号后接数个标记,其中可用的标记列表如下: % : 打印出 %:

    %n : 下一行
    %t : 跳格
    %H : 小时(00..23)
    %I : 小时(01..12)
    %k : 小时(0..23)
    %l : 小时(1..12)
    %M : 分钟(00..59)
    %p : 显示本地 AM 或 PM
    %r : 直接显示时间 (12 小时制,格式为 hh:mm:ss [AP]M)
    %s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数
    %S : 秒(00..61)
    %T : 直接显示时间 (24 小时制)
    %X : 相当于 %H:%M:%S
    %Z : 显示时区 %a : 星期几 (Sun..Sat)
    %A : 星期几 (Sunday..Saturday)
    %b : 月份 (Jan..Dec)
    %B : 月份 (January..December)
    %c : 直接显示日期与时间
    %d : 日 (01..31)
    %D : 直接显示日期 (mm/dd/yy)
    %h : 同 %b
    %j : 一年中的第几天 (001..366)
    %m : 月份 (01..12)
    %U : 一年中的第几周 (00..53) (以 Sunday 为一周的第一天的情形)
    %w : 一周中的第几天 (0..6)
    %W : 一年中的第几周 (00..53) (以 Monday 为一周的第一天的情形)
    %x : 直接显示日期 (mm/dd/yy)
    %y : 年份的最后两位数字 (00.99)
    %Y : 完整年份 (0000..9999)

    2.在设定时间方面:

    date -s //设置当前时间,只有root权限才能设置,其他只能查看。
    date -s 20080523 //设置成20080523,这样会把具体时间设置成空00:00:00
    date -s 01:01:01 //设置具体时间,不会对日期做更改
    date -s “01:01:01 2008-05-23″ //这样可以设置全部时间
    date -s “01:01:01 20080523″ //这样可以设置全部时间
    date -s “2008-05-23 01:01:01″ //这样可以设置全部时间
    date -s “20080523 01:01:01″ //这样可以设置全部时间

    3.加减:

    date +%Y%m%d         //显示前天年月日
    date +%Y%m%d --date="+1 day"  //显示前一天的日期
    date +%Y%m%d --date="-1 day"  //显示后一天的日期
    date +%Y%m%d --date="-1 month"  //显示上一月的日期
    date +%Y%m%d --date="+1 month"  //显示下一月的日期
    date +%Y%m%d --date="-1 year"  //显示前一年的日期
    date +%Y%m%d --date="+1 year"  //显示下一年的日期

    例子

    显示当前时间

    [root@layzj022301 ~]# date
    Mon Feb 27 23:54:13 CST 2017
    [root@layzj022301 ~]# date '+%c'
    Mon 27 Feb 2017 11:54:48 PM CST
    [root@layzj022301 ~]# date '+%D'
    02/27/17
    [root@layzj022301 ~]# date '+%x'
    02/27/2017
    [root@layzj022301 ~]# date '+%T'
    23:55:10
    [root@layzj022301 ~]# date '+%X'
    11:55:16 PM
    [root@layzj022301 ~]#

    格式化输出

    [root@layzj022301 ~]# date +"%Y-%m-%d"
    2017-02-27
    [root@layzj022301 ~]#

    输出昨天日期:

    [root@layzj022301 ~]# date -d "1 day ago" +"%Y-%m-%d"
    2017-02-26
    [root@layzj022301 ~]#

    设定时间:

    date -s                        //设置当前时间,只有root权限才能设置,其他只能查看 
    date -s 20170226               //设置成20120523,这样会把具体时间设置成空00:00:00 
    date -s 01:01:01               //设置具体时间,不会对日期做更改 
    date -s "01:01:01 2017-02-26"  //这样可以设置全部时间 
    date -s "01:01:01 20170226"    //这样可以设置全部时间 
    date -s "2017-02-26 01:01:01"  //这样可以设置全部时间 
    date -s "20170226 01:01:01"    //这样可以设置全部时间

    日期的加减操作

    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 --help

    [root@layzj022301 ~]# 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.
                                Example: Mon, 07 Aug 2006 12:34:56 -0600
          --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                                TIMESPEC=`date', `seconds', or `ns' for
                                date and time to the indicated precision.
                                Date and time components are separated by
                                a single space: 2006-08-07 12:34:56-06:00
      -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
    
    FORMAT controls the output.  Interpreted sequences are:
    
      %%   a literal %
      %a   locale's abbreviated weekday name (e.g., Sun)
      %A   locale's full weekday name (e.g., Sunday)
      %b   locale's abbreviated month name (e.g., Jan)
      %B   locale's full month name (e.g., January)
      %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
      %C   century; like %Y, except omit last two digits (e.g., 20)
      %d   day of month (e.g, 01)
      %D   date; same as %m/%d/%y
      %e   day of month, space padded; same as %_d
      %F   full date; same as %Y-%m-%d
      %g   last two digits of year of ISO week number (see %G)
      %G   year of ISO week number (see %V); normally useful only with %V
      %h   same as %b
      %H   hour (00..23)
      %I   hour (01..12)
      %j   day of year (001..366)
      %k   hour ( 0..23)
      %l   hour ( 1..12)
      %m   month (01..12)
      %M   minute (00..59)
      %n   a newline
      %N   nanoseconds (000000000..999999999)
      %p   locale's equivalent of either AM or PM; blank if not known
      %P   like %p, but lower case
      %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
      %R   24-hour hour and minute; same as %H:%M
      %s   seconds since 1970-01-01 00:00:00 UTC
      %S   second (00..60)
      %t   a tab
      %T   time; same as %H:%M:%S
      %u   day of week (1..7); 1 is Monday
      %U   week number of year, with Sunday as first day of week (00..53)
      %V   ISO week number, with Monday as first day of week (01..53)
      %w   day of week (0..6); 0 is Sunday
      %W   week number of year, with Monday as first day of week (00..53)
      %x   locale's date representation (e.g., 12/31/99)
      %X   locale's time representation (e.g., 23:13:48)
      %y   last two digits of year (00..99)
      %Y   year
      %z   +hhmm numeric timezone (e.g., -0400)
      %:z  +hh:mm numeric timezone (e.g., -04:00)
      %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
      %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
      %Z   alphabetic time zone abbreviation (e.g., EDT)
    
    By default, date pads numeric fields with zeroes.
    The following optional flags may follow `%':
    
      -  (hyphen) do not pad the field
      _  (underscore) pad with spaces
      0  (zero) pad with zeros
      ^  use upper case if possible
      #  use opposite case if possible
    
    After any flags comes an optional field width, as a decimal number;
    then an optional modifier, which is either
    E to use the locale's alternate representations if available, or
    O to use the locale's alternate numeric symbols if available.
    
    Report date bugs to bug-coreutils@gnu.org
    GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
    General help using GNU software: <http://www.gnu.org/gethelp/>
    For complete documentation, run: info coreutils 'date invocation'
    [root@layzj022301 ~]#
  • 相关阅读:
    解决:Could not resolve archetype org.apache.maven.archetypes
    Spring MVC配置MyBatis输出SQL
    Spring集成MyBatis 通用Mapper以及 pagehelper分页插件
    关于SpringMVC或Struts2接受参数接收不到的原因
    配置quartz启动时就执行一次
    ajaxFileUpload进行文件上传时,总是进入error
    spring mvc注入配置文件里的属性
    java中将一个文件夹下所有的文件压缩成一个文件
    flume failed to start agent because dependencies were not found in classpath
    ubuntu不能安装pip unable to install pip in unbuntu
  • 原文地址:https://www.cnblogs.com/xieshengsen/p/6478640.html
Copyright © 2011-2022 走看看