日期和时间命令使用

这里写图片描述

    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 ~]$date
Thu 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.35
Wed Nov 11 09:30:35 CST 2020

 

Clock

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

时钟的修改

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

[root@CentOS7 ~]$date 111109302020.35
Wed Nov 11 09:30:35 CST 2020
[root@CentOS7 ~]$clock -s
[root@CentOS7 ~]$date
Thu 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 ~]$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:      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) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
        TZ='Asia/Shanghai'; export TZ
to 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 you
can 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 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 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 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 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 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 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 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
       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 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值