One Linux command per day: Date command __linux

Source: Internet
Author: User
Tags set time date two weeks ago

In the Linux environment, whether it is programming or other maintenance, time is essential, but also often use the operation of time, skilled use of the date command to express the time they want to express, can certainly 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 system's dates and times. 3. Command parameters:

Necessary parameters:%H hours (expressed as 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 minutes, hours, 12 hours am/pm to express). Total number of seconds for%s. The commencement time is 1970-01-01 00:00:00 UTC. %s seconds (expressed in local idiomatic notation). %T time (including minutes, hours, the 24-hour system to express). %x time (expressed in local idiomatic notation). %Z City. %A week's abbreviation. The full name of the%A week. %b the initials of the English name of the month. %B the full English name of the month. %c date and time. Just entering the date directive 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, Yan analogy. The%x date (expressed in local idiomatic notation). %y year (expressed as 00-99). %Y year (in four-digit number). %n Inserts a new row when it is displayed. %T Inserts the TAB key when it is displayed. MM month (necessary) DD date (necessary) HH hours (necessary) mm minutes (necessary) SS seconds (optional)

Select parameters:-d< string > Display the date and time that the string refers to. Double quotes must be added before and after the string. -s< string > Sets the date and time according to the string. Double quotes must be added before and after the string. -U displays GMT. –help online Help. –version Displays version information 4. Use instructions:

In the display, the user can set the format to display, formatted as a plus after a number of tags, where the list of available tags is as follows:%: print%:
%n: Next line
%t: Jump
%H: Hours (00..23)
%I: Small When (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: Seconds to date from January 1, 1970 00:00:00 UTC
%s: sec (00..61)
%T: Direct display time (24-hour)
%x: Equivalent to%h:%m:%s
%Z: Display time zone%a: Day of the Week (Sun. Sat)
%A: Day of the Week (Sunday. Saturday)
%b: Month. DEC)
%B: Month (January. December)
%c: Show date and time directly
%d: Day (01..31)
%d: Direct display date (MM/DD/YY)
%h: Same%b
%j: Day of the Year (001..366)
%m: Month (01..12)
%u: The Week of the Year (00..53)
%w: Day of the Week (0..6)
%w: Weeks of the Year (00..53) (the first days of the week) (Monday as the first day of the week)
%x: Direct display date (MM/DD/YY)
%y: Last two digits of year (00.99)
%y: Full year (0000..9999)

In terms of setting the time:
Date-s//Set the current time, only root permission to set, others can only view.
Date-s 20080523//set to 20080523, this will set the specific time to empty 0:00:00
Date-s 01:01:01//Set specific time, do not make changes to date
Date-s "01:01:01 2008-05-23″//so you can set all the time
Date-s "01:01:01 20080523″//so you can set all the time
Date-s "2008-05-23 01:01:01″//so you can set all the time
Date-s "20080523 01:01:01″//so you can set all the time

Add and Subtract:
Date +%y%m%d//Show day before yesterday month
Date +%y%m%d–date= "+1 Day"//display of dates before
Date +%y%m%d–date= "-1 Day"//Displays the days after
Date +%y%m%d–date= "-1 month"//displays the dates of the previous January
Date +%y%m%d–date= "+1 month"//Show Next January
Date +%y%m%d–date= "-1 Year"//show dates of previous years
Date +%y%m%d–date= "+1 Year"//shows the next 5. Use instance:

Instance 1: Show Current time
Command:

Date '
+%c ' date '
+%d '
+%x
' date ' +%t ' Date ' +%x '

Output:

[root@localhost ~]# Date
December 08, 2012 Saturday 08:31:35 CST
[root@localhost ~]# date ' +%c '
December 08, 2012 Saturday 08:34 4 4 seconds
[root@localhost ~]# date ' +%d '
12/08/12[root@localhost ~]# date ' +%x '
December 08, 2012
[root@localhost ~]# Date ' +%t '
08:35:36[root@localhost ~]# date ' +%x '
08:35 54 seconds
[Root@localhost ~]#

Description

Example 2: Display date and set time
Command:

Date--date 08:42:00

Output:

[Root@localhost ~]# Date ' +%c '
December 08, 2012 Saturday 08:41 37 sec
[root@localhost ~]# date--date 08:42:00
2012 December 08 Saturday 08:42:00 CST
[root@localhost ~]# date ' +%c '--date 08:45:00
December 08, 2012 Saturday 08:45 00 sec
[Root@lo Calhost ~]#

Description

Instance 3:date-d parameter uses
Command:
Output:

[Root@localhost ~]# date-d "Nov"
Thursday, November 22, 2012 00:00:00 CST
[root@localhost ~]# date-d ' 2 weeks '
2012 December 22 Saturday 08:50:21 CST
[root@localhost ~]# date-d ' next Monday '
December 10, 2012 Monday 00:00:00 CST
[Root@loca Lhost ~]#  date-d next-day +%y%m%d
20121209[root@localhost ~]# date-d tomorrow +%y%m%d
20121209[ Root@localhost ~]# date-d last-day +%y%m%d 20121207[root@localhost ~]# date-d yesterday +%Y%m%d 20121207[
Root@localhost ~]# date-d last-month +%y%m 201211[root@localhost ~]# date-d next-month +%Y%m 201301[
Root@localhost ~]# date-d ' days ago ' 
November 08, 2012 Thursday 08:51:37 CST
[root@localhost ~]# date-d ' -100 days ' C15/>2012 year August 30 Thursday 08:52:03 CST
[root@localhost ~]#  date-d ' Dec 14-2 weeks '
November 30, 2012 Friday 00:00:0 0 CST
[root@localhost ~]# 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 parameter that encloses the date as a quotation mark. The-D option also tells you what day of the day is relative to the current date, a number of days or weeks from now, or before (past). You can accomplish this task by enclosing this relative offset in quotation marks as an argument to the-D option.

The details are as follows:
date-d "Nov 22" This year 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 (last month was a few months)
Date-d Next-month +%y%m (next month is a few months)

Using the AGO directive, you can get past dates:
Date-d ' day ago ' (30 days before the date)

Use negative numbers to get the opposite date:
Date-d ' Dec 14-2 weeks ' (relative: Dec 14 date two weeks ago)
Date-d ' -100 days ' (100 day ago date)
Date-d ' Day ' (50 days after date)

Example 4: Displays the number of months and days
Command:

Date  ' +%b%d '

Output:

[root@localhost ~]# date  ' +%b%d ' 
December 08[root@localhost ~]#

Description

Instance 5: Hop after display time, then show current date
Command:

Date ' +%t%n%d '

Output:

[Root@localhost ~]# Date ' +%t%n%d '
09:00:30
12/08/12[root@localhost ~]#

Description

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.