Linux date Plus and minus operations

Source: Internet
Author: User
Tags set time

In Linux shell programming, the addition and subtraction of dates is often used

View time:

[[Email protected] ~]# Date

Fri SEP 2 13:12:56 CST 2016

Modification Time:

[[email protected] ~]# date-s "1980-01-01 00:00:00"

Tue Jan 1 00:00:00 CST 1980

Actually, 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 day and time of the system.
1. In the display aspect, the user can set the format to be displayed, the format is set to a plus sign followed by a number of tags, where the list of available tokens is as follows:%: 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: 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%a: Day of the Week (Sun. Sat)
%A: Day of the Week (Sunday). Saturday)
%b: Month (Jan). DEC)
%B: Month (January). December)
%c: Display 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)

2. In the set time aspect
Date-s//Set the current time, only root permission can be set, others can only be viewed.
Date-s 20080523//set to 20080523, this will set the specific time to empty 00:00:00
Date-s 01:01:01//Set time, do not change the date
Date-s "01:01:01 2008-05-23″//This allows you to set the full time
Date-s 01:01:01 20080523″//So you can set the whole time
Date-s "2008-05-23 01:01:01″//so you can set the whole time
Date-s "20080523 01:01:01″//This can be set all the time
3. Add or subtract
Date +%y%m%d        // Displays the day of the month date
Date +%y%m%d--date= "+1 Day"  //Display date days after
date +%y%m%d--date= "-1 days"  //display date of the previous previous
dates +%y% m%d--date= "-1 month"  //Show dates on January
Date +%y%m%d--date= "+1 Month"  //Show date for next January
Date +%y%m%d--date= "-1 Year " //Displays the date of the previous day
date +%y%m%d--date=" +1 Year " //Displays the date of the following years

or simpler point of date= ' date-d-${t}day ' +%y%m%d '//For T for the first few days

Enterprise General Packaging method

[Email protected]/]# tar zcvf/tmp/test_$ (date +%f-%h%m%s) test

test/

test/oldboy/

test/oldboy/etc_2016-09-02

Test/test

[Email protected]/]# ls/tmp/

1.doc stu_102999_1.jpg test2016-09-02 TEST.SL

1.log stu_102999_2.jpg test2016-09-021210.tar.gz TEST.SL1

1.txt stu_102999_3.jpg test2016-09-02121107.tar.gz

data_2016-09-02 stu_102999_4.jpg test2016-09-02-121119.tar.gz

Ett stu_102999_5.jpg test_2016-09-02-132717


This article is from the "10543403" blog, please be sure to keep this source http://10553403.blog.51cto.com/10543403/1845549

Linux date Plus and minus operations

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.