Shell obtains the system time

Source: Internet
Author: User
Tags current greenwich mean time

Shell obtains the system time and returns the date + % Y-% m-% d of the current day. Output:, assign the value of myDate =$ (date + % Y % m % d) to the variable) assign the current DATE to the DATE variable date =$ (DATE + % Y % m % d). Sometimes we need to use the DATE before or after today, in this case, you can use the-d parameter of date to obtain tomorrow's date-d next-day + % Y % m % d to obtain the date of yesterday's 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 month date-d next-month + % Y % m get next year's year date-d next-year + % Y below are some date parameter descriptions and some example names: date permission: usage of all users: date [-u] [-d datestr] [-s datestr] [-- utc] [-- Universal] [-- date = datestr] [-- set = datestr] [-- help] [-- version] [+ FORMAT] [MMDDhhmm [[CC] YY] [. ss] Note: date can be used to display or set the date and time of the system. 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 list is as follows: Time: %: printed % n: Next row % t: Hop % H: hour (00 .. 23) % I: hour (01 .. 12) % k: hour (0 .. 23) % l: hour (1 .. 12) % M: minute (00 .. 59) % p: Local AM or PM % r: direct display time (in 12-hour format: hh: mm: ss [AP] M) % s: seconds from January 1, 1970 00:00:00 UTC till now % S: seconds (00 .. 61) % T: direct display time (in 24-hour format) % X: equivalent to % H: % M: % S % Z: Display Time Zone date aspect: %: 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: Display date (mm/dd/yy) % h: Same as % B % j: 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: Year The last two digits (00.99) % Y: The complete year (0000 .. 9999) if it does not start with a plus sign, it indicates the time to be set, and the time format is MMDDhhmm [[CC] YY] [. ss], where MM is the month, DD is the day, hh is the hour, mm is the minute, CC is the first two digits of the Year, YY is the last two digits of the Year, ss is the number of seconds to count: -d datestr: display the time set in datestr (non-system time) -- help: display the auxiliary message-s datestr: set the system time to the time set in datestr-u: display the current Greenwich Mean Time -- version: display version number example: show the time after the line jump, and then display the current date: date + % T % n % D display month and day number: date + % B % d display date and set time (12:34:56): date -- date 12:34:56 set the current system time (12:34:56): date -- s 12:34:56 Note: When you do not want meaningless 0 (for example,), you can insert the-symbol in the tag, such as date + %-H: %-M: %-S will remove the meaningless 0 in the hour, minute, and second, as if the original 08:09:04 will change. In addition, you can set the system time only when you have the permission (such as root. After you change the system time as root, remember to write the system time to CMOS using clock-w, in this way, the system time will continue to hold the latest correct value upon the next reboot. The ntp service is installed by default in the linux System for ntp time synchronization. Manual ntp synchronization is as follows: ntpdate ntp1.nl.net. Of course, you can also specify other ntp server token extended function date tools to do more work, it is not just to print out the current system date. You can use it to obtain the day of the week for the given date, and obtain the relative date relative to the current date. To learn about the day of the week, the GNU option is another extension to the date command. This option 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 the day of the week for a specific date by enclose the date as a parameter enclosed by quotation marks: $ date-d "nov 22" Wed Nov 22 00:00:00 EST 2006 $ in this example, you can see that July 22, November 22 is Wednesday this year. Therefore, if you hold a major meeting on July 15, November 22, you can immediately learn that this day is Wednesday and that day you will arrive at the resident office. You can also obtain the relative date d option to tell you which day is relative to the current date, from the current several days or after several weeks, or before (past ). You can complete this task by enclose the relative offset in quotation marks as a parameter of the-d option. For example, you need to know the date two weeks later. If you are at the Shell prompt, you can quickly get the answer: $ date-d '2 weeks 'There are other important methods to use this command. Use the next/last command to obtain the day of the week: $ date-d 'Next Monday' (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 (date of yesterday) or: date-d yesterday + % Y % m % d $ date-d last-month + % Y % m (last month is a few months) $ date-d next-month + % Y % m (the number of months in the next month) using the ago command, you can get the past date: $ date-d '30 days ago '(date before 30 days) you can use a negative number to get the opposite date: $ date-d 'dec 14-2 weeks' (relative: dec 14) $ date-d'-100 da Ys '(date before 100 days) $ date-d '50 days' (date after 50 days) is very useful. It can Set reminders for itself based on future dates, it may be in a script or Shell Startup File, as shown below: DAY = 'date-d' 2 weeks '+ "% B % d" 'If test "'echo $ Day'" = "Aug 16 "; then echo 'product launch is now two weeks away! '; Fi ############################## date format conversion in unix shell $ t_t = "Jul 1 21:29" $ date "+ % G-% m-% d % H: % M: % S "-d" $ t_t "# $ t_t format is relatively casual 21:29:00 $ date + % B/% d/% G-d" 2008-07-01 "Jul/01/2008 unix number of days between dates in shell $ expr '(' $ (date + % s-d "2008-07-02 ") -$ (date + % s-d "2008-05-30") '/86400 33 # If bcdate exists, you can directly use it. In shell, the unit of the specified interval for adding or subtracting dates is increased by 36 hours: $ a = 'date + % Y-% m-% d' $ B = 'date + % Y-% m-% d-d "$ a + 36 hours" '10 days ago: $ date-d "$ a-10 days" Sun Jun 22 00:00:00 CST 2008 displays the last date after the file is changed in the specified format, such as yyyy-mm-dd hh24: mi: ss $ date "+ % Y-% m-% d % H: % M: % S"-r test. bak 2008-07-01 21:28:55

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.