Detailed description of the date command in Linux

Source: Internet
Author: User
Tags current greenwich mean time
Name: Date
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, users can set the format to be displayed. The format is set to a plus sign followed by several tags, the available tags are listed as follows:
Time:
%: Printed
% N: Next row
% T: Skip
% H: hour (00 .. 23)
% I: hour (01 .. 12)
% K: hour (0 .. 23)
% L: hour (1 .. 12)
% M: minute (00 .. 59)
% P: displays local am or PM
% R: direct display time (in 12-hour format: hh: mm: ss [AP] m)
% S: number of 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 the time zone
Date:
% 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 directly
% D: Day (01 .. 31)
% D: Display date directly (mm/DD/yy)
% H: Same as % B
% J: The day of the year (001 .. 366)
% M: Month (01 .. 12)
% U: Week (00 .. 53) of the year (the first day of the week on Sunday)
% W: The day of the week (0 .. 6)
% W: Week (00 .. 53) of the Year (Monday is the first day of the week)
% X: Display date directly (mm/DD/yy)
% Y: last two digits of the Year (00.99)
% Y: full year (0000 .. 9999)
If it does not start with a plus sign, the time is set, and the time format is mmddhhmm [[CC] YY] [. SS].
MM indicates the month,
Dd is a day,
HH is the hour,
Mm is 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
Calculation:
-D datestr: display the time set in datestr (non-system time)
-- Help: displays auxiliary messages.
-S datestr: set the system time to the time set in datestr
-U: displays the current Greenwich Mean Time.
-- Version: displays the version number.
Example:
The current date is displayed: date + % T % N % d
Show month and day: 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: If you do not want meaningless 0 values (such as), you can insert a-symbol in the tag, such as date.
+ %-H: %-M: %-s will remove the meaningless 0 in the hour, as if the original 08:09:04 will change. In addition, only those who have the permission (for example
Root) to set the system time. After you change the system time as the root user, 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 at the next reboot.
NTP Time Synchronization
The NTP service is installed by default in Linux. Manual NTP synchronization is as follows:
Ntpdate ntp1.nl.net
Of course, you can also specify other NTP servers -------------------------------------------------------------------
The extended date tool can do more work, not just 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 the date two weeks before this date)
$ Date-d '-100 days' (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, possibly in scripts or shell startup files, as follows: Day = 'date-d' 2 weeks '+ "% B % d "'
If test "'echo $ Day'" = "Aug 16"; Then ECHO 'product launch is now two weeks away! '; Fi Original: http://hi.baidu.com/qishudong/blog/item/76a372aefc8fccfcfaed504e.html
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.