"Linux" Linux shell date formatting

Source: Internet
Author: User
Tags locale month name rfc set time time and seconds

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

Help information for the date command
[Email protected] source]# date--help
Usage: date [options] ... [+ format]
Or: Date [-u|--utc|--universal] [MMDDHHMM[[CC]YY][.SS]]
Displays the current time in the given format, or sets the system date.

-D,--date= string displays the time described by the specified string, not the current time
-F,--file= date file similar to--date, read-in time description by line from the date file
-R,--reference= file shows the last modified time for file specified file
-R,--rfc-2822 output date and time in RFC 2822 format
Example: August 7, 2006, Monday 12:34:56-0600
--rfc-3339=timespec outputs the date and time in RFC 3339 format.
timespec= ' Date ', ' seconds ', or ' ns '
Represents the display precision of a date and time.
The date and time units are separated by a single space:
2006-08-07 12:34:56-06:00
-S,--set= string sets the specified string to separate the time
-U,--UTC,--universal output or set Coordinated Universal Time
--HELP Display this help message and exit
--version display version information and exit

The given format controls the output, and the sequence is interpreted as follows:

Percent of a text%
%a of the current locale (ex: day, for Sunday)
%A the full name of the current locale (for example: Sunday)
%b the month name of the current locale (for example: one, for January)
%B the full name of the current locale (for example: January)
The date and time of the current locale of%c (for example: March 3, 2005 Thursday 23:05:25)
%c century; For example,%Y, typically omitting the last two digits of the current year (for example: 20)
%d date by month (ex: 01)
%d Date by month; equal to%m/%d/%y
%e date by month, add a space, equal to%_d
%F full date format, equivalent to%y-%m-%d
%g last two bits of the ISO-8601 format year (see%G)
%G ISO-8601 format year (see%V), typically used only with%V
%h equals%b.
%H hours (00-23)
%I Hours (00-12)
%c date by year (001-366)
%k (0-23)
%l (1-12)
%m Month (01-12)
%M points (00-59)
%n line break
%N nanosecond (000000000-999999999)
%p "Morning" or "afternoon" under current locale, output empty when unknown
%P is similar to%P, but outputs lowercase letters
%r 12-hour clock time at current locale (e.g. 11:11:04 pm)
%R 24 hours of time and minutes, equivalent to%h:%m
%s number of seconds since 1970-01-01 00:00:00 UTC time
%s seconds (00-60)
%t Output Tab tab
%T time, equal to%h:%m:%s
%u week, 1 stands for Monday
%u week of the year, with Sunday as the first day of the Week (00-53)
%V ISO-8601 Format Specification for the week of the year, with Monday as the first day of the Week (01-53)
%w Day of the Week (0-6), 0 for Monday
%W Week of the year, with Monday as the first day of every week (00-53)
Date description under the current locale of%x (for example: 12/31/99)
Time description Under current locale of%x (e.g. 23:13:48)
%y year last two digits (00-99)
%Y year
%z +HHMM Digital Time zone (for example,-0400)
%:z +hh:mm Digital Time zone (for example, -04:00)
%::z +HH:MM:SS Digital Time zone (for example, -04:00:00)
%:::z Digital time zone with the necessary precision (for example, -04,+05:30)
%Z time zone abbreviations by alphabet (for example, EDT)

Reference:

Http://www.cnblogs.com/try-better-tomorrow/p/5030640.html

Http://www.linuxidc.com/Linux/2013-03/81039.htm

"Linux" Linux shell date formatting

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.