Linux gets time in the shell

Source: Internet
Author: User

Tags: unit date assignment days ago version hint yourself Lin Shell

Get the date of the day

Date +%y-%m-%d

Output: 2011-07-28

Assigns the current date to a date variable
date=$ (Date +%y%m%d)

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 month
Date-d Next-month +%y%m

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

Here are some descriptions of the date parameters and some examples


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

##############################

Date format conversions in the Unix shell
$ t_t= "Jul 1 21:29"
$ date "+%g-%m-%d%h:%m:%s"-D "$t _t" # $t _t format is more casual
2008-07-01 21:29:00
$ date +%b/%d/%g-d "2008-07-01"
jul/01/2008

The number of days between dates in a Unix shell
$ Expr ' (' $ (date +%s-d "2008-07-02")-$ (date +%s-d "2008-05-30") ') '/86400
33
# #存在bcdate的话, can be used directly.

Shell date plus minus specified interval units
Add 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 June 00:00:00 CST 2008

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.bak
2008-07-01 21:28:55

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

Linux gets the time in the shell

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.

Tags Index: