Shell Date Time addition minus

Source: Internet
Author: User

1. Using the DATE-D option

Date + "%y%m%d"-D "+n days" today's post N day dates

Date + "%y%m%d"-D "-N days" today's top N Day dates

2. Common date format

Python

  1. [[email protected] tmp] # date + "%F"

  2. --

  3. [[email protected] tmp] # date + "%d"

  4. ///

  5. [[email protected] tmp] # date + "%y%m%d%h:%m:%s"

  6. 20120608 : £º

  7. [[email protected] tmp] # date + "%y%m%d"

  8. 20120608

  9. [[email protected] tmp] # date + "%y%m%d%h:%m:%s"-D "+1 hours"

  10. 20120608 : £º

  11. [[email protected] tmp] # date + "%y%m%d%h:%m:%s"-D "+1 days"

  12. 20120609 ::

  13. [[email protected] tmp] # date + "%y%m%d%h:%m:%s"-D "+1 months"

  14. 20120708 :

  15. [[email protected] tmp] # date + "%y%m%d%h:%m:%s"-D "+1 years"

  16. 20130608 : </pre><prename="code" class= "PHP">[[email protected] tmp]# date + "%T"

  17. in thefollowing:

  18. [[email protected] tmp] # date + "%F%T"

  19. A-on:

  20. [[email protected] tmp] # date + "%d%T"

Php

  1. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s"

  2. 2012-07-18 05:17:01

  3. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D "+10hour"

  4. 2012-07-18 15:17:16

  5. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D "+24hour"

  6. 2012-07-19 05:17:37

  7. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D " -24hour"

  8. 2012-07-17 05:17:45

  9. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D " -20minute"

  10. 2012-07-18 04:58:00

  11. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D "+20minute"

  12. 2012-07-18 05:38:05

  13. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D "+2day"

  14. 2012-07-20 05:18:15

  15. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D " -2day"

  16. 2012-07-16 05:18:19

  17. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D " -2month"

  18. 2012-05-18 05:18:24

  19. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D "+2month"

  20. 2012-09-18 05:18:28

  21. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D "+2year"

  22. 2014-07-18 05:18:32

  23. [[email protected] riqi]# date +"%y-%m-%d%h:%m:%s" -D " -2year"

  24. 2010-07-18 05:18:36

Using the shell to turn the time string and time stamp to each other

date +%s can get Unix timestamp;
use the shell to turn the time string with the timestamp:
date-d "2010-10-18 00:00:00" +%s output shape: 1287331200
and the time stamp is converted to a string to do this:
date-d @1287331200 "+%y-%m-%d" output shape such as: 2010-10-18
If you need to get the days before and after the specified date, you can:
1, seconds= ' date-d "2010-10-18 00:00:00" +%s ' #得到时间戳
2. seconds_new= ' expr $seconds + 86400 ' #加上一天的秒数86400
3, date_new= ' date-d @ $seconds _new "+%y-%m-%d" ' #获得指定日前加上一天的日前

This article is from the "Guangzhou Linux operation and Maintenance" blog, please be sure to keep this source http://milenovo.blog.51cto.com/10554394/1694592

Shell Date Time addition minus

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.