Linux Timestamp and Time Difference Calculation

Source: Internet
Author: User

Http://www.sunjianhe.com /? P = 760

Use the timestamp to convert the date output to a specific format. The output format is: date-D '2017-01-01 UTC timestamp seconds '+ "% Y-% m-% d % T % Z"

[Root @ mail161-Centos ~] # Date
Wed Oct 27 09:49:22 CST 2010
[Root @ mail161-Centos ~] # Date + % s
1288144164
[Root @ mail161-Centos ~] # Date-D '2017-01-01 UTC 1970 seconds '+ "% Y-% m-% d % T % Z"
09:49:24 + 0800

Get tomorrow's date: Date-d "1 days" + % d or date-d "tomorrow" + % d

Get the date of yesterday: Date-d "1 days ago" + % d or date-d "yesterday" + % d

Get the date of today: Date-d "0 days" + % d or date-d "now" % d

Get the date n days after today: Date-d "n Days" + % d

Get the date n days before today: Date-d "n days ago" + % d

Where N is a specific integer

Similar usage: Date-D "1 years" + % Y get the year after a year
Date-d "1 years ago" + % Y get how many years a year ago

The following are frequently used in shell processing:

1. Time addition and subtraction

The processing method here is to convert the Basic time to the timestamp, and then increase or change the time to the second.

For example, 01:01:01 plus 1 hour and 20 minutes

Solution:

A. Convert the Basic time to the timestamp

Time1 = $ (date + % s-d '2017-01-01 01:01:01 ′)

Echo $ time1

631126861 [timestamp]

B. Change the increase time to seconds.

[Root @ localhost ~] # Time2 = $(1*60*60 + 20*60 ))
[Root @ localhost ~] # Echo $ time2

4800

C. Calculate the result time by adding two times.

Time1 = $ ($ time1 + $ time2 ))

Time1 = $ (date + % Y-% m-% d \ % H: % m: % s-d "1970-01-01 UTC $ time1 seconds ");

Echo $ time1

02:21:01

2. Time Difference Calculation Method

For example, the time difference between and 11:11:11

Principle: convert it to a timestamp and calculate the day, hour, minute, and second.

Time1 = $ (date + % s-d '2017-01-01 ')-$ (date + % s-d '2017-01-01 11:11:11 ′)));

Echo time1

Change time1/60 seconds to minute.

Note:

Shell single-bracket operator number:

A = $ (date );

Equivalent to: a = 'date ';

Double brackets OPERATOR:

A = $(1 + 2 ));

Echo $;

Equivalent:

A = 'expr 1 + 2'

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.