Sometimes we need to use dates before or after today, so we can use the-D parameter of date

Get Tomorrow's date

Date-d Next-day +%y%m%d

Get yesterday's date

Date-d Last-day +%y%m%d

Get last month's year and month

Date-d Last-month +%y%m

Get next month's year and monthDate-d Next-month +%y%m

Get the year next yearDate-d Next-year +%y

Here are some descriptions of the date parameters and some examples

Name: DateUsage rights: All usersHow to use: Date [-u] [-D DATESTR] [-S datestr] [--UTC] [--universal] [--DATE=DATESTR] [--SET=DATESTR] [--help] [--version] [+f ORMAT] [MMDDHHMM[[CC]YY][.SS]]Description: Date can be used to display or set the system's Day and time, in terms of display, the user can set the format to be displayed, the format is set to a plus followed by a number of tags, where the list of available tags is as follows:In terms of time:%: 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: The 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 zoneDate aspects:%a: Day of the Week (Sun. Sat)%A: Day of the Week (Sunday). Saturday)%b: Month (Jan). DEC)%B: Month (January). December)%c: Displays the 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)If you do not start with a plus sign, you want to set the time, and the time format is MMDDHHMM[[CC]YY][.SS],Where MM is the month,DD is the day,HH for hours,MM for minutes,CC is the first two digits of the year,YY is two digits after the year,SS is the number of secondsPut the meter:-D datestr: Displays the time set in the DATESTR (non-system time)--HELP: Display Auxiliary message-S DATESTR: Set the system time to the time set in Datestr-U: Show current GMT--version: Show version numberExample:Show time after Skip, then show current date: Date +%t%n%dDisplay month and day: Date +%b%dDisplay date and set time (12:34:56): Date--date 12:34:56Set system Current Time (12:34:56): Date--s 12:34:56Note: When you do not want to appear meaningless 0 o'clock (for example, 1999/03/07), you can insert a symbol in the tag-for example, date +%-h:%-m:%-s will remove the meaningless 0 of the time and seconds, as if the original 08:09:04 will become 8:9:4. In addition, the system time can be set only if the authority (for example, Root) is acquired. When you change the system time as root, remember to use CLOCK-W to write the system time to the CMOS so that the system time will continue to hold the latest correct value the next time you reboot.NTP time synchronizationThe NTP service is installed by default under the Linux system and is manually ntp synchronized as followsNtpdate ntp1.nl.netOf course, you can also specify a different NTP server-------------------------------------------------------------------Extended FunctionalityThe date tool can do more work than just print out the current system date. You can use it to get a given date exactly what the day of the week is, and get a relative date compared to the current date. Know what day of the weekAnother extension of the GNU to the date command is the-D option, which is useful when there is no Calendar table on your table (UNIX users do not need a calendar table). With this powerful option, you can quickly find out exactly what day of the week a particular date is by providing a date as a parameter in quotation marks:$ date-d "Nov 22"Wed 00:00:00 EST 2006$In this example, you can see that this year's November 22 is Wednesday.So, assuming that a major meeting is held on November 22, you can immediately learn that the day is Wednesday, and that day you will arrive at the resident office.Get relative datesThe D option can also tell you what day it is, relative to the current date, several days or weeks from now onwards, or before (past). This is done by enclosing this relative offset in quotation marks and as a parameter to the-D option.For example, you need to know the date after two weeks. If you are at the Shell prompt, you can get an answer quickly:$ Date-d ' 2 weeks 'There are some other important ways to use this command. Using the Next/last directive, you can get what day of the week after:$ Date-d ' next Monday ' (date of next Monday)$ 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 (months last month)$ date-d next-month +%y%m (next month is a few months)With the ago command, you can get past dates:$ Date-d ' day ago ' (30 days before date)You can use negative numbers to get the opposite date:$ Date-d ' Dec 14-2 weeks ' (relative: Dec 14 date of two weeks before this date)$ Date-d ' -100 days ' (100 day prior to date)$ Date-d ' Day ' (50 days after date)This technique is useful for setting up reminders for yourself based on future dates, possibly in a script or Shell startup file, as follows:Day= ' date-d ' 2 weeks ' + "%b%d" 'if test "' echo $DAY '" = "16"; Then Echo ' Product launch was now weeks away! '; Fi

Displays the last date after a file change in the specified format, such as Yyyy-mm-dd Hh24:mi:ss$ date "+%y-%m-%d%h:%m:%s"-R Test.bak2008-07-01 21:28:55

From:http://www.xuebuyuan.com/1122682.html

Linux gets the time in the shell

This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to info-contact@alibabacloud.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.

Try 40+ Products For Free !

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.