Usage of the Linux Date command
In Linux shell programming, the addition and subtraction of dates is often used
I used to use the expr function to calculate the problem.
Actually, 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 day and time of the system.
1. In the display aspect, the user can set the format to be displayed, the format is set to a plus sign followed by a number of tags, where the list of available tokens is as follows:%: Print out%
%n: Next line
%t: Jump Grid
%H: Hours (00..23)
%I: Hours (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: Number of seconds from January 1, 1970 00:00:00 UTC to date
%s: Seconds (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 (Jan). DEC)
%B: Month (January). December)
%c: Display date and time directly
%d: Day (01..31)
%d: Show date directly (MM/DD/YY)
%h: With%b
%j: The first day of the Year (001..366)
%m: Month (01..12)
%u: Week of the Year (00..53) (in Sunday as the first day of the week)
%w: The day of the Week (0..6)
%W: Week of the Year (00..53) (with Monday as the first day of the week)
%x: Show date directly (MM/DD/YY)
%y: Last two digits of the year (00.99)
%Y: Full year (0000..9999)
2. In terms of setting time
Date-s//Set current time, only root privileges can be set, others can only be viewed.
Date-s 20080523//set to 20080523, this will set the specific time to empty 00:00:00
Date-s 01:01:01//Set time, no changes made to date
Date-s "01:01:01 2008-05-23″//so you can set the whole time
Date-s "01:01:01 20080523″//so you can set the whole time
Date-s "2008-05-23 01:01:01″//so you can set the whole time
Date-s "20080523 01:01:01″//so you can set the whole time
3. Add and Subtract
Date +%y%m%d//display now day and Month day
Date +%y%m%d--date= "+1 Day"//Display days later
Date +%y%m%d--date= "-1 days"//Displays the day before
Date +%y%m%d--date= "-1 month"//show dates on January
Date +%y%m%d--date= "+1 Month"//displays the dates for the next January
Date +%y%m%d--date= "-1 Year"//show dates for previous years
Date +%y%m%d--date= "+1 Year"//show dates for next
# t=3 T for defining variables
date-d-${t}day ' +%y%m%d '//For T for the first few days
This article is from the "CENTOS6 Learning" blog, please make sure to keep this source http://wskfnso.blog.51cto.com/4025557/1946871
Date command usage for Linux