One Linux command per day: Date command

Source: Internet
Author: User
Tags set time

In the Linux environment, whether it is programming or other maintenance, time is essential, but also often use the time of the operation, skilled use of the date command to express the time you want to express, certainly can give their work to bring many convenience.

1. Command format:

Date [parameter] ... [+ format]

2. Command function:

Date can be used to display or set the day and time of the system.

3. Command parameters:

Necessary parameters:

%H hours (expressed in 00-23).

%I Hours (expressed in 01-12).

%K hours (expressed in 0-23).

%l hours (expressed in 0-12).

%M minutes (expressed in 00-59).

%P am or PM.

%r time (including hours, minutes and seconds, hour is expressed as 12 hours am/pm).

Total number of seconds in%s. The starting time is 1970-01-01 00:00:00 UTC.

%s seconds (as represented by local idioms).

%T time (minutes and seconds, hours are expressed in 24-hour system).

%x time (expressed in local idiomatic notation).

%Z downtown.

%a abbreviation of the week.

The full name of the%A week.

Abbreviation for%b month English name.

The full English name of the%B month.

%c date and time. Entering only the date instruction will also show the same result.

%d date (expressed as 01-31).

%d Date (including month date).

%j the first day of the year.

%m month (expressed as 01-12).

%u the number of weeks in the year.

%w the number of days of the week, 0 for Sunday, 1 for Monday, once analogy.

%x date (expressed in local idiomatic notation).

%y year (expressed in 00-99).

%Y year (expressed in four digits).

%n inserts a new line when it is displayed.

%t when displayed, Insert tab.

MM Month (required)

DD Date (required)

HH Hour (necessary)

MM min (required)

SS sec (Selective)

Select parameters:

-d< string > Displays the date and time that the string refers to. Double quotation marks must be added before and after strings.

-s< string > Sets the date and time based on the string. Double quotation marks must be added before and after strings.

-U displays GMT.

--help online Help.

--version displaying version information

4. Instructions for use:

1. In the display aspect, the user can set the format to be displayed, the format is set to a plus sign followed by a number of tags, where the list of available tokens is as follows:%: Print out%:

%n: Next line

%t: Jump Grid

%H: Hours (00..23)

%I: Hours (01..12)

%k: Hours (0..23)

%l: Hours (1..12)

%M: minutes (00..59)

%p: Show local AM or PM

%r: Direct Display time (12-hour format, HH:MM:SS [ap]m)

%s: Number of seconds from January 1, 1970 00:00:00 UTC to date

%s: Seconds (00..61)

%T: Direct Display time (24-hour system)

%x: Equivalent to%h:%m:%s

%Z: Show time zone%a: Day of the Week (Sun. Sat)

%A: Day of the Week (Sunday). Saturday)

%b: Month (Jan). DEC)

%B: Month (January). December)

%c: Display date and time directly

%d: Day (01..31)

%d: Show date directly (MM/DD/YY)

%h: With%b

%j: The first day of the Year (001..366)

%m: Month (01..12)

%u: Week of the Year (00..53) (in Sunday as the first day of the week)

%w: The day of the Week (0..6)

%W: Week of the Year (00..53) (with Monday as the first day of the week)

%x: Show date directly (MM/DD/YY)

%y: Last two digits of the year (00.99)

%Y: Full year (0000..9999)

2. In terms of setting the time:

Date-s//Set current time, only root privileges can be set, others can only be viewed.

Date-s 20080523//set to 20080523, this will set the specific time to empty 00:00:00

Date-s 01:01:01//Set time, no changes made to date

Date-s "01:01:01 2008-05-23″//so you can set the whole time

Date-s "01:01:01 20080523″//so you can set the whole time

Date-s "2008-05-23 01:01:01″//so you can set the whole time

Date-s "20080523 01:01:01″//so you can set the whole time

3. Add and Subtract:

Date +%y%m%d//Display day of the day before date

Date +%y%m%d--date= "+1 days"//Display the day before

Date +%y%m%d--date= "-1 days"//day after display

Date +%y%m%d--date= "-1 month"//show dates on January

Date +%y%m%d--date= "+1 Month"//displays the dates for the next January

Date +%y%m%d--date= "-1 Year"//show dates for previous years

Date +%y%m%d--date= "+1 Year"//show dates for next

5. Usage examples:

Example 1: Display the current time

Command:

Date

Date ' +%c '

Date ' +%d '

Date ' +%x '

Date ' +%t '

Date ' +%x '

Output:

[[Email protected] ~]# Date

Saturday, December 08, 2012 08:31:35 CST

[[Email protected] ~]# Date ' +%c '

December 08, 2012 Saturday 08:34 44 seconds

[[Email protected] ~]# Date ' +%d '

12/08/12[[email protected] ~]# date ' +%x '

December 08, 2012

[[Email protected] ~]# Date ' +%t '

08:35:36[[email protected] ~]# date ' +%x '

08:35 54 sec

[Email protected] ~]#

Description

Example 2: Display date and set time

Command:

Date--date 08:42:00

Output:

[[Email protected] ~]# Date ' +%c '

December 08, 2012 Saturday 08:41 37 seconds

[[Email protected] ~]# date--date 08:42:00

Saturday, December 08, 2012 08:42:00 CST

[[Email protected] ~]# date ' +%c '--date 08:45:00

December 08, 2012 Saturday 08:45 00 seconds

[Email protected] ~]#

Description

Instance 3:date-d parameter use

Command:

Output:

[Email protected] ~]# date-d "Nov 22"

Thursday, November 22, 2012 00:00:00 CST

[[email protected] ~]# date-d ' 2 weeks '

Saturday, December 22, 2012 08:50:21 CST

[Email protected] ~]# date-d ' next Monday '

Monday, December 10, 2012 00:00:00 CST

[Email protected] ~]# date-d next-day +%y%m%d

20121209[[email protected] ~]# date-d tomorrow +%y%m%d

20121209[[email protected] ~]# date-d last-day +%y%m%d

20121207[[email protected] ~]# date-d yesterday +%y%m%d

20121207[[email protected] ~]# date-d last-month +%y%m

201211[[email protected] ~]# date-d next-month +%y%m

201301[[email protected] ~]# date-d ' days ago '

Thursday, November 08, 2012 08:51:37 CST

[[email protected] ~]# date-d ' -100 days '

Thursday, August 30, 2012 08:52:03 CST

[Email protected] ~]# date-d ' Dec 14-2 weeks '

Friday, November 30, 2012 00:00:00 CST

[Email protected] ~]# date-d ' days '

Sunday, January 27, 2013 08:52:27 CST

Description

Another extension of the date command is the-D option, which is useful. With this powerful option, you can quickly pinpoint a specific date by providing a date as a parameter in quotation marks. The-D option can also tell you what day it is, relative to the current date, several days or weeks from now onwards, or before (past). This is done by enclosing this relative offset in quotation marks and as a parameter to the-D option.

Specify the following:

date-d "Nov 22" This year's November 22 is Wednesday

Date-d ' 2 weeks ' 2 weeks after the date

Date-d ' next Monday ' (date of next Monday)

Date-d Next-day +%y%m%d (tomorrow's date) or: Date-d tomorrow +%y%m%d

Date-d Last-day +%y%m%d (yesterday's date) or: date-d yesterday +%y%m%d

date-d last-month +%y%m (months last month)

Date-d Next-month +%y%m (next month is a few months)

With the ago command, you can get past dates:

Date-d ' days ago (30 day before date)

Use negative numbers to get the opposite date:

Date-d ' Dec 14-2 weeks ' (relative: Dec 14 date Two weeks before this date)

Date-d ' -100 days ' (100 day old date)

Date-d ' Day ' (50 days after the date)

Example 4: Display the month and the number of days

Command:

Date ' +%b%d '

Output:

[[Email protected] ~]# Date ' +%b%d '

December 08[[email protected] ~]#

Description

Example 5: Display the time after skip, and then display the current date

Command:

Date ' +%t%n%d '

Output:

[Email protected]host ~]# Date ' +%t%n%d '

09:00:30

12/08/12[[email protected] ~]#

One Linux command per day: Date command

Related Article

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.