In Linux shell programming, date addition and subtraction operations are often used
I used to calculate myself through the expr function, which is troublesome.
In fact, the date command itself provides the addition and subtraction of dates.
Very convenient. For example: Get yesterday's time
Date +%y%m%d--date= "-1 day"
Date usage: date [OPTION] ... [+format]
Date [-u|--utc|--universal] [MMDDHHMM[[CC]YY][.SS]]
Date can be used to display or set the system's dates and times.
Order 1. In the display, the user can set the format to display, format set to a plus after a number of tags, where the list of available tags is as follows:%: Print out%
%n: Next line
%t: Jump Lattice
%H: Hours (00..23)
%I: Hours (01..12)
%k: Hours (0..23)
%l: Hours (1..12)
%m: Min (00..59)
%p: Show local AM or PM
%r: Direct Display time (12-hour system, format hh:mm:ss [ap]m)
%s: Number of seconds from January 1, 1970 00:00:00 UTC to date
%s: sec (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. DEC)
%B: Month (January. December)
%c: Show date and time directly
%d: Day (01..31)
%d: Direct display date (MM/DD/YY)
%h: With%b
%j: The first day of the Year (001..366)
%m: Month (01..12)
%u: The week of the Year (00..53) (with Sunday as the first day of the week)
%w: The first day of the Week (0..6)
%w: The first few weeks of the year (00..53) (in Monday case for day one of the week)
%x: Direct display date (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 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 00: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
3. 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"//display the dates after
Date +%y%m%d--date= "-1 month"//display of dates on 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"//show the dates of the following years