Linux Get Time Date method

Source: Internet
Author: User
Tags set time time and seconds

Use the shell in Linux to get yesterday, tomorrow, or many days ago:
In Linux, the man date-d parameter is relatively vague, the following examples further illustrate:
#-D,--date=string display time described by STRING, not ' now '
[Email protected] root]# date-d next-day +%y%m%d #明天日期
20091024
[Email protected] root]# date-d last-day +%y%m%d #昨天日期
20091022
[Email protected] root]# date-d yesterday +%y%m%d #昨天日期
20091022
[[email protected] root]# date-d tomorrow +%y%m%d # tomorrow's date
20091024
[Email protected] root]# date-d last-month +%y%m #上个月日期
200909
[Email protected] root]# date-d next-month +%y%m #下个月日期
200911
[Email protected] root]# date-d next-year +%y #明年日期
2010
date=$ (Date +%y%m%d--date ' 2 days ago ') #获取昨天或多天前的日期
Name: Date
Usage rights: All users
How 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 zone
Date 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 seconds
Put 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 number
Example:
Show time after Skip, then show current date: Date +%t%n%d
Display month and day: 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: 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 synchronization
The NTP service is installed by default under the Linux system and is manually ntp synchronized as follows
Ntpdate ntp1.nl.net
Of course, you can also specify a different NTP server
-------------------------------------------------------------------
Extended Functionality
The 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 week
Another 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 dates
The 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

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.