"Linux Common Commands" time command

Source: Internet
Author: User
Tags set time time and date

Date Command

Date the command is to display or set the system time and date.

many Shell The script needs to print a different format of time or date, and to perform actions based on time and date. Latency is typically used during script execution to provide a period of time to wait. Dates can be printed in multiple formats, or you can use commands to set a fixed format. In the class unix system, the date is stored as an integer, which is sized from the world standard Time ( UTC ) 1970 year 1 month 1 Day 0 0 sub 0

Grammar

date(选项)(参数)

Options

-d<字符串>:显示字符串所指的日期与时间。字符串前后必须加上双引号;

-s<字符串>:根据字符串来设置日期与时间。字符串前后必须加上双引号;

-u:显示GMT;

--help:在线帮助;

--version:显示版本信息。

Parameters

<+ Time Date format > : Specifies the date and time format to use when displaying.

List of date format strings

%H 小时,24小时制(00~23)

%I 小时,12小时制(01~12)

%k 小时,24小时制(0~23)

%l 小时,12小时制(1~12)

%M 分钟(00~59)

%p 显示出AM或PM

%r 显示时间,12小时制(hh:mm:ss %p)

%s 从1970年1月1日00:00:00到目前经历的秒数

%S 显示秒(00~59)

%T 显示时间,24小时制(hh:mm:ss)

%X 显示时间的格式(%H:%M:%S)

%Z 显示时区,日期域(CST)

%a 星期的简称(Sun~Sat)

%A 星期的全称(Sunday~Saturday)

%h,%b 月的简称(Jan~Dec)

%B 月的全称(January~December)

%c 日期和时间(Tue Nov 20 14:12:58 2012)

%d 一个月的第几天(01~31)

%x,%D 日期(mm/dd/yy)

%j 一年的第几天(001~366)

%m 月份(01~12)

%w 一个星期的第几天(0代表星期天)

%W 一年的第几个星期(00~53,星期一为第一天)

%y 年的最后两个数字(1999则是99)

Instance

Formatted output:

date +"%Y-%m-%d"

2009-12-07

Output Yesterday Date:

date -d "1 day ago" +"%Y-%m-%d"

2012-11-19

in the legendary 1234567890 seconds:

date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%m:%S"

2009-02-13 23:02:30

Normal turn format:

date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"

2009/12/12 00:00.00

Set Time:

date -s                        //设置当前时间,只有root权限才能设置,其他只能查看

date -s 20120523               //设置成20120523,这样会把具体时间设置成空00:00:00

date -s 01:01:01               //设置具体时间,不会对日期做更改

date -s "01:01:01 2012-05-23"  //这样可以设置全部时间

date -s "01:01:01 20120523"    //这样可以设置全部时间

date -s "2012-05-23 01:01:01"  //这样可以设置全部时间

date -s "20120523 01:01:01"    //这样可以设置全部时间

Hwclock Command

Hwclock The command is a hardware clock access tool that can display the current time, set the hardware clock time and set the hardware clock as the system time, or set the time of the system time to the hardware clock.

in the Linux There are two kinds of clocks, such as hardware clock and system clock. The hardware clock is the clock device on the motherboard, which is usually the clock that can be set on the BIOS screen. The system clock refers to the clock in the kernel. When Linux starts, the system clock reads the settings of the hardware clock, and then the system clock runs independently. All Linux -related instructions and functions are programmed to read the system clock.

Grammar

hwclock(选项)

Options

--adjust:hwclock每次更改硬件时钟时,都会记录在/etc/adjtime文件中。使用--adjust参数,可使hwclock根据先前的记录来估算硬件时钟的偏差,并用来校正目前的硬件时钟;

--debug:显示hwclock执行时详细的信息;

--directisa:hwclock预设从/dev/rtc设备来存取硬件时钟。若无法存取时,可用此参数直接以I/O指令来存取硬件时钟;

--hctosys:将系统时钟调整为与目前的硬件时钟一致;

--set --date=<日期与时间>:设定硬件时钟;

--show:显示硬件时钟的时间与日期;

--systohc:将硬件时钟调整为与目前的系统时钟一致;

--test:仅测试程序,而不会实际更改硬件时钟;

--utc:若要使用格林威治时间,请加入此参数,hwclock会执行转换的工作;

--version:显示版本信息。

Instance

The hardware time to be set depends on the operating system time, as follows:

hwclock systohc

hwclock --systohc -utc

use without any parameters Hwclock , you can view the current hardware date and time.

hwclock

View Clock file to confirm that you have set the UTC :

cat
 /etc/default/rcS 

UTC=yes

in some other versions of the Linux (such as Rebhat ) can be viewed in this way:

cat /etc/sysconfig/clock

ZONE="America/Los_Angeles"

UTC=false

ARC=false

Cal Command

Cal command to display the current calendar, or a calendar that specifies a date.

Grammar

cal(选项)(参数)

Options

-l:显示单月输出;

-3:显示临近三个月的日历;

-s:将星期日作为月的第一天;

-m:将星期一作为月的第一天;

-j:显示"julian"日期;

-y:显示当前年的日历。

Parameters

月:指定月份;

年:指定年份。

Instance

performed separately Cal the command will print out the calendar:

[[email protected] ~]# cal

    十二月 2013     

日 一 二 三 四 五 六

 1  2  3  4  5  6  7

 8  9 10 11 12 13 14

20 21

22 23 24 25 26 27 28

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.