日期和时间命令使用

来源:互联网 发布:mix软件是什么 编辑:程序博客网 时间:2024/05/18 07:53

这里写图片描述

    Linux中分为系统时间和硬件时间,系统时间使用date命令和机器时间使用clock命令,本文就简单介绍一下Linux中的时间和日期的命令。

Date

直接在命令行中输入date命令,显示的是系统时间。系统时间是由Linux内通过cpu的工作频率的计时。

命令格式:

(1)显示日期和时间

  date [OPTION]... [+FORMAT]
        FORMAT:
            %F:按照指定的格式显示。如:2017-07-15
            %D:直接显示日期mm/dd/yy
            %H:显示小时,如:0-23
            %M:显示分钟
            %S:显示秒
            %s:显示从1970年1月1日到现在所经过的秒数
            %m:显示月份

[root@CentOS7 ~]$dateThu Aug 31 22:09:47 CST 2017[root@CentOS7 ~]$date +"%F %T"2017-08-31 22:09:50

(2)修改日期和时间

  date [-u|-utc|--universal] [MMMDDHHMM[[CC]YY][.ss]]
        MM:月份
        DD:几号
        hh:小时
        mm:分钟
        CC:两位年份
        CCYY:四位年份
        ss:秒

[root@CentOS7 ~]$date 111109302020.35Wed Nov 11 09:30:35 CST 2020

 

Clock

clock或者hwclock的输出结果是硬件时间。硬件时间就是主板BIOS提供的时钟有主板电池供电来维持运行。

时钟的修改

    修改时钟包括系统时间和硬件时间
    hwlock用法
      -s , –hctosys 以硬件时间为准同步到系统时间
      -w, –systohc 以系统时间为准同步到硬件时间

[root@CentOS7 ~]$date 111109302020.35Wed Nov 11 09:30:35 CST 2020[root@CentOS7 ~]$clock -s[root@CentOS7 ~]$dateThu Aug 31 22:12:09 CST 2017

 

ntpdate

ntpdate命令后面跟时钟服务器的IP地址,可以让系统同步来自服务器的时间

  如:ntpdate 172.18.0.1

[root@CentOS7 ~]$ntpdate 172.18.0.1 31 Aug 22:23:55 ntpdate[27159]: step time server 172.18.0.1 offset 37.242806 sec

 

更改时区

要更改 Linux 系统时区首先得获知你所当地时区的 TZ 值,使用 tzselect 命令即可查看并选择已安装的时区文件。

[root@CentOS7 ~]$tzselectPlease 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 Ocean10) Pacific Ocean11) none - I want to specify the time zone using the Posix TZ format.#? 5Please 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) Taiwan10) Cyprus                27) Lebanon               44) Tajikistan11) East Timor            28) Macau                 45) Thailand12) Georgia               29) Malaysia              46) Turkmenistan13) Hong Kong             30) Mongolia              47) United Arab Emirates14) India                 31) Myanmar (Burma)       48) Uzbekistan15) Indonesia             32) Nepal                 49) Vietnam16) Iran                  33) Oman                  50) Yemen17) Iraq                  34) Pakistan#? 9Please select one of the following time zone regions.1) Beijing Time2) Xinjiang Time#? 1The following information has been given:        China        Beijing TimeTherefore TZ='Asia/Shanghai' will be used.Local time is now:      Thu Aug 31 22:17:07 CST 2017.Universal Time is now:  Thu Aug 31 14:17:07 UTC 2017.Is the above information OK?1) Yes2) No#? 1You can make this change permanent for yourself by appending the line        TZ='Asia/Shanghai'; export TZto the file '.profile' in your home directory; then log out and log in again.Here is that TZ value again, this time on standard output so that youcan use the /bin/tzselect command in shell scripts:Asia/Shanghai

可以通过选择数字的方式按照大洲-国家-城市,一步一步选择到你想设置的时区。
 

Cal

cal命令是显示当前日历,默认是当前月份的日历。

[root@CentOS7 ~]$cal     August 2017    Su Mo Tu We Th Fr Sa       1  2  3  4  5 6  7  8  9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31

cal 年份:可以显示年份日历

[root@CentOS7 ~]$cal 2022                               2022                                      January               February                 March       Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa                   1          1  2  3  4  5          1  2  3  4  5 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 1916 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 2623 24 25 26 27 28 29   27 28                  27 28 29 30 3130 31        April                   May                   June        Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa                1  2    1  2  3  4  5  6  7             1  2  3  4 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 1110 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 1817 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 2524 25 26 27 28 29 30   29 30 31               26 27 28 29 30        July                  August                September     Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa                1  2       1  2  3  4  5  6                1  2  3 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 1010 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 1717 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 2424 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 3031       October               November               December      Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa                   1          1  2  3  4  5                1  2  3 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 1716 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 2423 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 3130 31
原创粉丝点击