In Linux Shell programming, addition and subtraction of dates are often used.
Previously, it was very troublesome to use expr function computing.
In fact, the date command itself provides the addition and subtraction operation of the date
Very convenient. For example, get the time of yesterday
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 date and time of the system.
1. in terms of display, you can set the format to be displayed. The format is set to a plus sign followed by several tags. The available tag lists are as follows: %: Print %
% N: next row
% T: Hop size
% H: hour (00 .. 23)
% I: hour (01 .. 12)
% K: hour (0 .. 23)
% L: hour (1 .. 12)
% m: minute (00 .. 59)
% P: Display local am or PM
% R: display time directly (in 12-hour format: hh: mm: ss [AP] m)
% s: the number of seconds since 00:00:00 UTC on January 1, January 1, 1970
% s: seconds (00 .. 61)
% T: direct display time (in 24-hour format)
% x: % H: % m: % S
% Z: display Time Zone % A: The day of the week (sun .. sat)
% A: The day of the week (Sunday .. saturday)
% B: Month (Jan .. dec)
% B: Month (January .. december)
% C: Display date and time
% d: Day (01 .. 31)
% d: Date (mm/DD/yy)
% H: Same as % B
% J: the day of the year (001 .. 366)
% m: Month (01 .. 12)
% u: Week (00 .. 53) (Sunday is the first day of a week)
% W: The day of a week (0 .. 6)
% W: Week (00 .. 53) (Monday is the first day of a week)
% x: Date (mm/DD/yy)
% Y: last two digits of the Year (00.99)
% Y: full year (0000 .. (9999)
2. Set the time
date-S // set the current time. Only the root permission can be set. Others can only be viewed.
date-s 20080523 // set to 20080523, so that the specific time is set to null 00:00:00
date-s 01:01:01 // set the specific time, no changes will be made to the date
date-s "01:01:01 2008-05-23" // you can set the full time
date-s "01:01:01 20080523" // you can set the full time
date-s "01:01:01" // you can set the full time
date-s "20080523 01:01:01" // you can set the full time
3. plus or minus
date + % Y % m % d // display the day before yesterday
date + % Y % m % d -- date = "+ 1 day" // display the day before
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