date command instructions

1, Command Overview

  After the command is a date or date and time setting systems, in the display, the user can set the format to be displayed, the format is set to take a number of plus marks.
  If not as the beginning of a plus sign, it means to set the time, and time format MMDDhhmm [[CC] YY] [ . Ss], where MM is the month, DD is the day, hh is hours, mm minutes, CC for the year the first two digits, YY is the year of two digits, ss is the number of seconds.
  Many shell scripts which need to print a different time or date formats, and to perform operations according to the time and date. Delay is typically used during script execution to wait for some time provided. Date to be printed in various formats, commands may be used to set a fixed format.

2, the command syntax

date [options] [+] output format

3, command options

-d <string>: date and time display string referred to. Before and after the string must be enclosed in double quotes;
-s <string>: to set the date and time according to the character string. Before and after the string must be double quotation marks;

date + Time Date format: date time format used when displaying:

%%: print out%
% A: a few days of the week (Sun..Sat)
% A: full name (Sunday..Saturday) day of the week
% b: abbreviations of the months (Jan..Dec)
% B: January full name (January..December)
% c: date time (Sat Nov 04 12:02:33 EST 1989)
% C: century (after 100 years in addition to whole) [00-99]
% d: the first few month day (01..31)
% D: date (mm / dd / YY)
% E: the day of the month (1..31)
% F.: date, with the Y-% M-% D%
% G: Year (YY)
% G: Year (YYYY)
% H: with% b, abbreviations of the months (Jan..Dec)
% H: 24 hour (00..23)
% the I: 12 is hour (01..12 ), # uppercase I
% J: day of the year (001..366)
% K: 24 hour (0..23)
% L: 12 is hour (1..12), lowercase L #
% m: month (01..12)
% m: min (00..59)
% n-: newline
% N: ns (000000000..999999999)
% P: the PM or AM
% P: AM or PM
% R & lt: 12-hour time (HH: mm: SS [the AP] M)
% R & lt: 24-hour time (hh: mm)
% s: starting from 00:00:00 1970-01-01 UTC seconds
% S: seconds (00..60)
% T: Tab
% T: 24-hour time (HH: mm: SS)
% u: day of the week (1..7); 1 represents Monday
% U: the first few weeks of the year, Sunday is the first day (00 .. 53) per week
% V: the first year weeks, Monday is the first day (01..53) weekly
% w: day of the week (0..6); 0 represents Sunday
% W: the first few weeks of the year, a week Monday the first day (00 .. 53)
% X: date (mm / dd / YY)
% X-: time (% H:% M:% S AM)
% Y: Year after two (00..99)
% Y: Year (1970 ...)
% Z: when the RFC 2822-style digital format region (-0500)
% the Z: the time zone (eg, EDT), null zone can not be determined

Commonly used:
% the Y: Year (1970 ...)
% m: month (01..12)
% D: day of the month (01..31)
% H: hour (00..23)
% M: min (00..59)
% S: seconds (00..60)
% F.: date, with the Y-% M-% D%
% T: 24-hour time (hh: mm: ss)

Common combinations:
DATE "+ M-% D%%% Y-H:% H:% S"
DATE "% T +% F."

4, an example of command

4.1 -d option to use:

1) Output tomorrow Date:

1 [root@lzg ~]# date
2 Tue Nov  5 10:37:37 CST 2019
3 [root@lzg ~]# date -d "1 day"
4 Wed Nov  6 10:37:44 CST 2019
5 [root@lzg ~]# date -d "1 day" "+%Y-%m-%d %H:%M:%S"
6 2019-11-06 10:37:55

2) Output 5 days Date:

1 [root@lzg ~]# date -d "5 day"
2 Sun Nov 10 10:38:34 CST 2019
3 [root@lzg ~]# date -d "5 day" "+%Y-%m-%d %H:%M:%S"
4 2019-11-10 10:38:38

3) Output time after 1000 seconds:

1 [root@lzg ~]# date
2 Tue Nov  5 10:39:04 CST 2019
3 [root@lzg ~]# date -d "1000 second"
4 Tue Nov  5 10:55:54 CST 2019
5 [root@lzg ~]# date -d "1000 second" "+%Y-%m-%d %H:%M:%S"
6 2019-11-05 10:56:02

4) 2 years after the date of output:

1 [root@lzg ~]# date
2 Tue Nov  5 10:40:49 CST 2019
3 [root@lzg ~]# date -d "2 year"
4 Fri Nov  5 10:40:51 CST 2021
5 [root@lzg ~]# date -d "2 year" "+%Y-%m-%d %H:%M:%S"
6 2021-11-05 10:41:00

5) Output 1 day before, five days before the time, before time 1000 seconds, 2 years time:

1 [root@lzg ~]# date -d "1 day ago" "+%Y-%m-%d %H:%M:%S"
2 2019-11-04 10:42:25
3 [root@lzg ~]# date -d "5 day ago" "+%Y-%m-%d %H:%M:%S"
4 2019-10-31 10:42:29
5 [root@lzg ~]# date -d "1000 second ago" "+%Y-%m-%d %H:%M:%S"
6 2019-11-05 10:26:02
7 [root@lzg ~]# date -d "2 year ago" "+%Y-%m-%d %H:%M:%S"
8 2017-11-05 10:42:57

6) subtraction operation (sec, min, hour, day, weeks, month, year):

. 1 DATE the Y% m%% + D displays the current date # 
 2 DATE -d " + 1'd Day " + m%%% the Y D # display the previous day 
 . 3 DATE -d " -1 Day " +% the Y% m% after the day's date display d # 
 . 4 dATE -d " -1 month the " + the Y% m%% d # display on January dates 
 . 5 dATE -d " + 1'd month the " + the Y% m%% d # display the date of the next month 
 . 6 dATE -d " -1 year " + m%%% the Y D # display the date of the previous year 
 . 7 dATE -d " + 1'd year " + m%%% the Yd # display date year
 . 8 DATE -d " +5 hour " + the Y% m% D%% H:% M:% S # 5 hours after the displayed time

[LZG the root @ ~] -d # DATE "5hour + + + 5day 10min" "+% Y-M-% D%% H:% M:% S"      #. 5 days and 5 hours after 10 minutes
2019-11 -10 16:49:34

[LZG the root @ ~] -d # DATE "-5day -5hour -10min" "+% Y-M-% D%% H:% M:% S"      # 10 minutes before 5 days 5 hours
2019-10 -31 06:35:28

7) a common format conversion:

1 [root@lzg ~]# date -d "20090225" "+%Y-%m-%d"
2 2009-02-25
3 [root@lzg ~]# date -d "20090225" "+%Y-%m-%d %H:%M:%S"
4 2009-02-25 00:00:00

8) apache format conversion:

1 [root@lzg ~]# date -d "Dec 9, 2010 10:00:37 AM" "+%Y-%m-%d %H:%M:%S"
2 2010-12-09 10:00:37

9) Add Time format conversion:

1 [root@lzg ~]# date -d "Dec 9, 2010 10:00:37 AM 2 year" "+%Y-%m-%d %H:%M:%S"
2 2012-12-09 10:00:37

Use 4.2 -s option (to set the date and time):

. 1 [LZG the root @ ~ ] DATE #
 2 Tue-Nov   . 5  10 : 44 is : 29 CST 2019 
. 3 [LZG the root @ ~] -s # DATE " 2011-01-10 12:22:22 "       # modified
 . 4 Mon Jan 10  12 is : 22 is : 22 is CST 2011 
. 5 [LZG the root @ ~ ] # DATE
 . 6 Mon Jan 10  12 is : 22 is : 26 is CST 2011
1 DATE - S           # Set the time, only root privileges to set up, only to see the other 
 2 DATE -s 20120523  # set to 20,120,523, which will set the specific time to empty 00:00: 0 0  
3 DATE -s 01 : 01 : 01 # set a specific time, do not change the date of 
 4 dATE -s " 01:01:01 2012-05-23 " # this sets all the time 
 5 dATE -s " 01:01:01 20,120,523 "    # this sets all the time 
 6 DATE -s " 2012-05-23 01:01:01 " # this sets all-time 
 7 DATE -s " 20,120,523 01:01:01 "    # this sets all the time

Date plus 4.3 Description:

1 [root@lzg ~]# date "+Today is %Y-%m-%d %H:%M:%S"
2 Today is 2019-11-05 11:13:05

 

Guess you like

Origin www.cnblogs.com/liuzgg/p/11797521.html