date那些事儿

一、date

说明:按照指定格式查看系统的时间,通过date也可以设置系统的时间;

常用参数

  -s:修改系统时间指定时间格式;

  -d:按照指定格式显示指定的时间,非系统时间;

 %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; (使用空格填充显示一个月份中的第几天)
  %F   full date; same as %Y-%m-%d;
  %T   time; same as %H:%M:%S;
  %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)(一年中的第几天)
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline(在显示日期之前开始新的一行)
  %N   nanoseconds (000000000..999999999)(显示纳秒数)
  %p   locale's equivalent of either AM or PM;显示的是AM还是PM;
  %P   like %p, but lower case(显示am或者pm)
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M(24小时制显示时间)
  %s   seconds since 1970-01-01 00:00:00 UTC(显示从1970年01月01秒数)
  %S   second (00..60)(显示秒数)
  %t   a tab(一个制表位)
  %u   day of week (1..7); 1 is Monday(显示星期中的第几天,1代表的是星期一)
  %w   day of week (0..6); 0 is Sunday(显示星期中的第几天,0代表星期日)
  %V   ISO week number, with Monday as first day of week (01..53)(国际标准化组织定义的一年中的第几个星期,星期一作为一周中的第一天)
 
  %U   week number of year, with Sunday as first day of week (00..53)(显示一年中的第几个星期,星期日作为星期中的第一天)
  %W   week number of year, with Monday as first day of week (00..53)(显示一年中的第几个星期,星期一作为星期中的第一天)
​
  %y   last two digits of year (00..99)(显示年的最后两位数)
  %Y   year

 代码演示: 

[root@doctor-10 ~]# date "+%F %T"
2020-02-05 11:22:17
[root@doctor-10 ~]# date "+%A"
Wednesday
[root@doctor-10 ~]# date "+%a"
Wed
[root@doctor-10 ~]# date "+%b"
Feb
[root@doctor-10 ~]# date "+%B"
February
[root@doctor-10 ~]# date "+%G"
2020
[root@doctor-10 ~]# date "+%g"
20
[root@doctor-10 ~]# date "+%r"
10:46:32 AM
[root@doctor-10 ~]# date "+%R"
10:46
[root@doctor-10 ~]# date "+%U"
05
[root@doctor-10 ~]# date "+%W"
05
[root@doctor-10 ~]# date "+%V"
06

二、Linux修改时间

 

  1. 使用date -s 修改时间

    [root@doctor-10 ~]# date -s "2020/02/05 11:24"
  2. 使用tzselect命令进行修改时间

    注意tzselect 命令属于/usr/bin/tzselect,不在系统环境变量中,需自己配置,如果不配置,可使用绝对路径使用命令;

    按照系统提示进行操作即可完成设置时间;

    [root@doctor-10 ~]# /usr/bin/tzselect 
    Please identify a location so that time zone rules can be set correctly.
    Please select a continent or ocean.
     1) Africa
     2) Americas
     3) Antarctica
     4) Arctic Ocean
     5) Asia
     6) Atlantic Ocean
     7) Australia
     8) Europe
     9) Indian Ocean
    10) Pacific Ocean
    11) none - I want to specify the time zone using the Posix TZ format.
    #? 5
    Please select a country.
     1) Afghanistan       18) Israel            35) Palestine
     2) Armenia       19) Japan         36) Philippines
     3) Azerbaijan        20) Jordan            37) Qatar
     4) Bahrain       21) Kazakhstan        38) Russia
     5) Bangladesh        22) Korea (North)     39) Saudi Arabia
     6) Bhutan        23) Korea (South)     40) Singapore
     7) Brunei        24) Kuwait            41) Sri Lanka
     8) Cambodia          25) Kyrgyzstan        42) Syria
     9) China         26) Laos          43) Taiwan
    10) Cyprus        27) Lebanon           44) Tajikistan
    11) East Timor        28) Macau         45) Thailand
    12) Georgia       29) Malaysia          46) Turkmenistan
    13) Hong Kong         30) Mongolia          47) United Arab Emirates
    14) India         31) Myanmar (Burma)       48) Uzbekistan
    15) Indonesia         32) Nepal         49) Vietnam
    16) Iran          33) Oman          50) Yemen
    17) Iraq          34) Pakistan
    #? 9
    Please select one of the following time zone regions.
    1) Beijing Time
    2) Xinjiang Time
    #? 1
    ​
    The following information has been given:
    ​
        China
        Beijing Time
    ​
    Therefore TZ='Asia/Shanghai' will be used.
    Local time is now:  Wed Feb  5 11:34:09 CST 2020.
    Universal Time is now:  Wed Feb  5 03:34:09 UTC 2020.
    Is the above information OK?
    1) Yes
    2) No
    #? 
  3. 使用timeconfig修改时间;(需安装)

    yum install system-config-date 
  4. CenoOS7可以使用命令timedatectl设置时间;

    timedatectl set-timezone Asia/Shanghai
  5. 通过覆盖/etc/localtime或创建软连接修改时间

    cp /usr/share/zoneinfo/$主时区/$次时区  /etc/localtime  
    eg:配置CST时间: 
        cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime 
    创建软连接
        ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime 
  6. 通过ntpdate设置时间

    [root@doctor-10 ~]# yum install ntp
    [root@doctor-10 ~]#ntpdate -u ntp.api.bz

    ntpdate命令介绍

    说明:set the date and time via NTP(通过ntp服务器设置时间)
    常用参数: -u Direct ntpdate to use an unprivileged port for outgoing packets. This is most useful when behind a firewall that blocks incoming traffic to privileged ports, and you want to synchronize with hosts beyond the firewall. Note that the -d option always uses unprivileged ports.
  7. ntp常用服务器:

    中国ntp服务器:cn.pool.ntp.org
    上海交通大学网络中心NTP服务器:ntp.sjtu.edu.cn
    NTP服务器(上海) :ntp.api.bz
    北京邮电大学 : s1a.time.edu.cn
    清华大学 : s1b.time.edu.cn
    北京大学 : s1c.time.edu.cn
    美国: time.nist.gov
    复旦: ntp.fudan.edu.cn
    微软公司授时主机(美国) :time.windows.com
    台警大授时中心(台湾):asia.pool.ntp.org

 

posted @ 2020-02-05 13:03  夜凌云~  阅读(164)  评论(0编辑  收藏  举报