Summary of common methods for time operations in bash shell

Source: Internet
Author: User

In daily work, the bash shell has a very frequent time operation. For example, when the shell script sends statistics periodically, it will check whether the scheduled send time is current. Or when you use a file to save some data, a time string is typically generated as part of the file name. Summing up, later use will not have to surf the internet everywhere.

<1> the most basic operation, get the current time (year-month-day time: minutes: seconds):

Date + "%y-%m-%d%h:%m:%s"

%I Hours, 12-hour system

%s from January 1, 1970 00:00:00 to the number of seconds currently experienced

%a Week's (SUN~SAT)

%b month abbreviation (JAN~DEC)

<2> time before or after the specified interval

3 hours before the time date-d "1 hour" + "%y-%m-%d%h:%m:%s"

3 hours after the time date-d "+1 hour" + "%y-%m-%d%h:%m:%s"

3 days prior to date-d "-1 day" + "%y-%m-%d%h:%m:%s"

Take 3 days after date-d "+1 Day" + "%y-%m-%d%h:%m:%s"

Take 1 months ago date-d "1 month" + "%y-%m-%d%h:%m:%s"

1 months after the time date-d "+1 month" + "%y-%m-%d%h:%m:%s"

Take 1 years ago date-d "1 year" + "%y-%m-%d%h:%m:%s"

1 years after the time date-d "+1 year" + "%y-%m-%d%h:%m:%s"

<3> Timestamp and string conversion

String to timestamp date-d "2018-08-15 21:33:00"-S

Timestamp to String date-d "1970-01-01 UTC 1534337485 seconds" + "%y-%m-%d%h:%m:%s"

<4> time Addition and subtraction

This seems to only turn the string into a timestamp, add and subtract, in the conversion back to the string, I found no simple way.

Summary of common methods for time operations in bash 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.

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.