The use of the date command in Linux shell programming

Source: Internet
Author: User
Tags date current time linux

In Linux shell programming, date addition and subtraction operations are often used

I used to calculate myself through the expr function, which is troublesome.

In fact, the date command itself provides the addition and subtraction of dates.

Very convenient. For example: Get yesterday's time

Date +%y%m%d--date= "-1 day"

Date usage: date [OPTION] ... [+format]

Date [-u|--utc|--universal] [MMDDHHMM[[CC]YY][.SS]]

Date can be used to display or set the system's dates and times.

Order 1. In the display, the user can set the format to display, format set to a plus after a number of tags, where the list of available tags is as follows:%: Print out%

%n: Next line

%t: Jump Lattice

%H: Hours (00..23)

%I: Hours (01..12)

%k: Hours (0..23)

%l: Hours (1..12)

%m: Min (00..59)

%p: Show local AM or PM

%r: Direct Display time (12-hour system, format hh:mm:ss [ap]m)

%s: Number of seconds from January 1, 1970 00:00:00 UTC to date

%s: sec (00..61)

%T: Direct Display time (24-hour system)

%x: Equivalent to%h:%m:%s

%Z: Show time zone%a: Day of the Week (Sun. Sat)

%A: Day of the Week (Sunday. Saturday)

%b: Month. DEC)

%B: Month (January. December)

%c: Show date and time directly

%d: Day (01..31)

%d: Direct display date (MM/DD/YY)

%h: With%b

%j: The first day of the Year (001..366)

%m: Month (01..12)

%u: The week of the Year (00..53) (with Sunday as the first day of the week)

%w: The first day of the Week (0..6)

%w: The first few weeks of the year (00..53) (in Monday case for day one of the week)

%x: Direct display date (MM/DD/YY)

%y: Last two digits of the year (00.99)

%Y: Full year (0000..9999)

2. In terms of setting the time

Date-s//Set the current time, only root permission to set, others can only view.

Date-s 20080523//set to 20080523, this will set the specific time to empty 00:00:00

Date-s 01:01:01//Set specific time, do not make changes to date

Date-s "01:01:01 2008-05-23″//so you can set all the time

Date-s "01:01:01 20080523″//so you can set all the time

Date-s "2008-05-23 01:01:01″//so you can set all the time

Date-s "20080523 01:01:01″//so you can set all the time

3. Add and Subtract

Date +%y%m%d//Show day before yesterday month

Date +%y%m%d--date= "+1 Day"///display of dates before

Date +%y%m%d--date= "-1 Day"//display the dates after

Date +%y%m%d--date= "-1 month"//display of dates on January

Date +%y%m%d--date= "+1 Month"//Show Next January

Date +%y%m%d--date= "-1 Year"//show dates of previous years

Date +%y%m%d--date= "+1 Year"//show the dates of the following years

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.