Linux common commands: date command


  In the Linux environment, whether programming or other maintenance, time is essential, and time operations are often used. Skilled use of the date command to express the time you want to express will definitely bring a lot to your work. Convenience.

1. Command format:

  date [parameters]... [+format]

2. Command function:

  date can be used to display or set the system date and time.

3. Command parameters:

Required parameters:

%H hours (expressed as 00-23). 

%I hours (expressed as 01-12). 

%K hours (expressed as 0-23). 

%l hours (expressed as 0-12). 

%M minutes (expressed as 00-59). 

%P AM or PM. 

%r Time (including hours, minutes and seconds, and hours are expressed in 12-hour AM/PM). 

%s Total seconds. The start time is 1970-01-01 00:00:00 UTC. 

%S seconds (in local idiom). 

%T Time (including hours, minutes and seconds, and hours are expressed in 24-hour format). 

%X time (in local idiom). 

%Z urban area. 

%a Abbreviation for week. 

%A The full name of the week. 

%b Abbreviation for the English name of the month. 

The full English name of the month %B. 

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

%d Date (represented as 01-31). 

%D Date (including year, month and day). 

%j Day of the year. 

%m Month (as 01-12). 

%U Week number in the year. 

%w The number of days in the week, 0 for Sunday, 1 for Monday, and so on. 

%x date (in local idiom). 

%y Year (as 00-99). 

%Y Year (in four digits). 

%n When displayed, insert a new line. 

%t Insert tab when displayed. 

MM month (required) 

DD date (required) 

hh hours (required) 

mm minutes (necessary)

ss seconds (optional) 

Select parameters:

-d <string> Displays the date and time the string refers to. Strings must be enclosed in double quotes. 

-s<string> Set date and time according to string. Strings must be enclosed in double quotes. 

-u Display GMT. 

--help Online help. 

--version display version information 

4. Instructions for use:

1. In terms of display, the user can set the format to be displayed. The format is set to a plus sign followed by several marks. The list of available marks is as follows: % : print out %:

%n : next line

%t : tab

%H : hour (00..23)

%I : hour (01..12)

%k : hour (0..23)

%l : hours (1..12)

%M : minutes (00..59)

%p : show local AM or PM

%r : Display time directly (12-hour clock, format hh:mm:ss [AP]M)

%s : seconds since 1/1/1970 00:00:00 UTC

%S : seconds (00..61)

%T : Display time directly (24-hour clock)

%X : equivalent to %H:%M:%S

%Z : Display timezone %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 : Display date directly (mm/dd/yy)

%h : same as %b

%j : day of the year (001..366)

%m : month (01..12)

%U : Week of the year (00..53) (if Sunday is the first day of the week)

%w : day of the week (0..6)

%W : Week of the year (00..53) (if Monday is the first day of the week)

%x : Display date directly (mm/dd/yy)

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

%Y : full year (0000..9999)

2. In terms of setting time:

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

date -s 20080523 //Set to 20080523, which will set the specific time to empty 00:00:00

date -s 01:01:01 //Set the specific time, the date will not be changed

date -s "01:01:01 2008-05-23" //This can set the full time

date -s "01:01:01 20080523" //This can set the full time

date -s "2008-05-23 01:01:01" //This can set the full time

date -s "20080523 01:01:01" //This can set the full time

3. Addition and subtraction:

date +%Y%m%d //Display the year, month and day of the previous day

date +%Y%m%d --date="+1 day" //Display the date of the previous day

date +%Y%m%d --date="-1 day" //Display the date of the next day

date +%Y%m%d --date="-1 month" //Display the date of the previous month

date +%Y%m%d --date="+1 month" //Display the date of the next month

date +%Y%m%d --date="-1 year" //Display the date of the previous year

date +%Y%m%d --date="+1 year" //Display the date of the next year

5. Example of use:

Example 1: Display the current time

Order:

date

date '+%c'

date '+%D'

date '+%x'

date '+%T'

date '+%X'

output:

[root@localhost ~]# date
Saturday , December 08 , 
2012 08:31:35 CST

[root@localhost ~]# date '+%c'

Sat Dec 08, 2012 08:34:44

[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

[root@localhost ~]#

 

illustrate:

Example 2: Display date and set time

Order:

  date --date 08:42:00

output:

[root@localhost ~]# date '+%c'

Sat Dec 08, 2012 08:41:37

[root@localhost ~]# date --date 08:42:00
Saturday , December 08 , 
2012 08:42:00 CST

[root@localhost ~]# date '+%c' --date 08:45:00

Sat Dec 08, 2012 08:45:00

[root@localhost ~]#

 

illustrate:

Example 3: use of date -d parameter

Order:

output:

[root@localhost ~]# date -d "nov 22"
Thursday , November 22 , 
2012 00:00:00 CST

[root@localhost ~]# date -d '2 weeks'
Saturday , December 22 , 
2012 08:50:21 CST

[root@localhost ~]# date -d 'next monday'
Monday , December 10 , 
2012 00:00:00 CST

[root@localhost ~]#  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 '30 days ago' 
Thu , 08 Nov 
2012 08:51:37 CST

[root@localhost ~]# date -d '-100 days' 
Thursday , August 30 , 
2012 08:52:03 CST

[root@localhost ~]#  date -d 'dec 14 -2 weeks'
Fri , 30 Nov 
2012 00:00:00 CST

[root@localhost ~]# date -d '50 days'
Sunday , January 27 , 
2013 08:52:27 CST

illustrate:

  Another extension to the date command is the -d option, which is very useful. Using this powerful option, you can quickly pinpoint a specific date by supplying the date as an argument enclosed in quotes. The -d option can also tell you which day is a number of days from the current date, a number of days or weeks from now, or before (in the past). This is accomplished by enclosing this relative offset in quotes as an argument to the -d option.

The specific instructions are as follows:

date -d "nov 22" this year's november 22 is a wednesday

date -d '2 weeks' date in 2 weeks

date -d 'next monday' (next monday's date)

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 (what month is the last month)

date -d next-month +%Y%m (what month is the next month)

Using the ago directive, you can get past dates:

date -d '30 days ago' (date 30 days ago)

Use negative numbers to get the opposite date:

date -d 'dec 14 -2 weeks' (relative: the date two weeks before the date of dec 14)

date -d '-100 days' (dates 100 days ago)

date -d '50 days' (date after 50 days)

 

Example 4: Displaying Month and Day

Order:

  date  '+%B %d'

output:

[root@localhost ~]# date  '+%B %d' 

Dec 08 [root@localhost ~]#

 

illustrate:

 

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

Order:

  date '+%T%n%D'

output:

[root@localhost ~]# date '+%T%n%D'

09:00:30

12/08/12[root@localhost ~]#

illustrate:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325205747&siteId=291194637