How to use the date command in linux to obtain the time of yesterday or tomorrow

Source: Internet
Author: User
The date command can get the current time. man shows that many parameters of date can be used, which is easy to format.

For example:


Copy codeThe code is as follows:
Date + "% F"
Output Format: 2011-12-31

Date + "% F % H: % M: % S"
Output Format: 2011-12-31 16:29:50


This is to print the current time of the system. if you want to obtain a time relative to the current time, you can use the-d parameter. For example:


Copy codeThe code is as follows:
Date-d "tomorrow" + "% F % H: % M: % S"
Output the time at this time tomorrow

Date-d "yesterday" + "% F % H: % M: % S"
Output the time at this time yesterday

If I want to get the time of 13 days ago, the-d parameter has more flexible usage, for example:


Copy codeThe code is as follows:
Date-d "-1 day ago" + "% F % H: % M: % S"
Output the time at this time tomorrow

Date-d "1 day ago" + "% F % H: % M: % S"
Output the time at this time yesterday

Date-d "1 week ago" + "% F % H: % M: % S"
Output the time seven days ago, which is equivalent
Date-d "7 day ago" + "% F % H: % M: % S"

We can see that ago is powerful. The first number can be a negative number, and the negative number indicates the future time. the positive number indicates the past time, the second parameter is minute, hour, day, month, and week.

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.