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 to my variable $ (date + % Y % m % d) assign the current DATE to the DATE variable date $ (DATE + % Y % m % d). sometimes we need to use the DATE before or after today. at this time, we can... 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-mo Nth + % Y % m get next year's year date-d next-year + % Y below are some date parameter descriptions and some example names: date usage permission: all users usage: 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: 1 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: displays the date (mm/dd/yy) % y: The last two digits of the year (00.99) % Y: full 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: skip the line after the display time, 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 system current time (12:34:56): date -- s 12:34:56 note: if you do not want meaningless 0 values (for example,), you can insert the-symbol in the tag, for example, date + %-H: %-M: %-S removes 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

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.