Usage of the Linux Date command

Source: Internet
Author: User
Tags set time string format

In Linux shell programming, adding and subtraction operations that are often used for dates are previously calculated by using the expr function, which is cumbersome. In fact, the date command itself provides a convenient addition and subtraction of dates.

For example: Get yesterday's time
Date--date= "-1 day" +%y%m%d

Date Usage: date [OPTION] ... [+format] The
Date [-u|--utc|--universal] [MMDDHHMM[[CC]YY][.SS]]
Date can be used to display or set the date and time of the system.
1. In the display, the user can set the format to be displayed, formatted as a plus followed by a number of tokens, where the list of available tokens is as follows:%:  print out%
%n: Next line
%t: Jump grid
% H: Hours (00..23)
%I: Small Time (01..12)
%k: Hours (0..23)
% L: Hours (1..12)
% M: minutes (00..59)
%p: Show local AM or PM
%r: Direct display times (12-hour format, HH:MM:SS [AP] M)
%s: The number of seconds since January 1, 1970 00:00:00 UTC
% s: seconds (00..61)
% T: Direct display time (24 hour)
% X: equivalent to%h:%m:%s
%Z: Explicit 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: Day of the Year (001..366)
%m: Month Copy (01..12)
% U: Week of the Year (00..53) (with Sunday as the first day of the week)
%w: Day of the Week (0..6)
% W: Week of the Year (00..53) (Monday is the first week of Days)
%x: Display date directly (MM/DD/YY)
%y: Last two digits of the year (00.99)
% y: Full year (0000..9999)

2. In terms of setting time
Date-s//Set current time, only root privileges 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, no changes made to date
Date-s "01:01:01 2008-05-23″//so you can set the whole 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″//so you can set the whole time
3. Add and Subtract
Date +%y%m%d//display now day and Month day
Date +%y%m%d--date= "+1 Day"//Display days later
Date +%y%m%d--date= "-1 days"//Displays the day before
Date +%y%m%d--date= "-1 month"//show dates on January
Date +%y%m%d--date= "+1 Month"//displays the dates for the next January
Date +%y%m%d--date= "-1 Year"//show dates for previous years
Date +%y%m%d--date= "+1 Year"//show dates for next

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

string format time to timestamp

We know that the date command can directly convert the current time to a timestamp

# date +%s
1436781527
And here we turn our defining time into time.

# date-j-F "%y-%m-%d%h:%m:%s" "2015-07-13 18:02:00" "+%s"
1436781720
In fact this slightly complicated if hand hit

Time stamp to time

But the time stamp becomes readable time form is very simple, Mac and CentOS a little different

On Mac

date-r1436781720
July 13, 2015 Monday 18:02 00 SEC CST
CentOS on $ date-d @1436781720
Monday, July 13, 2015 18:02:00 CST
These two commands are really short and good to remember

Usage of the Linux Date command

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.