Subtraction operation of time and date in shell

Source: Internet
Author: User
Tags iso 8601 time and date

First, let's start by saying what the shell timestamp is:

The number of seconds since January 1, 1970 (00:00:00 utc/gmt). It is also known as the Unix timestamp (Unix timestam, Unix epoch, POSIX time, Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (Midnight of Utc/gmt), regardless of the leap seconds.

The Unix timestamp of 0 according to the ISO 8601 specification is: 1970-01-01t00:00:00z One hours is expressed as a UNIX timestamp format of: 3,600 seconds; one day is represented as a Unix timestamp of 86,400 seconds, and leap seconds is not counted.

Time subtraction under the shell is done based on timestamps, and timestamps are very useful for our operations under the shell:

For example: Calculate the time stamp of a day, that is, the number of seconds specified by one to January 1, 1970:

Total seconds from 2014-12-05 19:45:44 to 1970-1-1 [[email protected] shell]# date-d "2014-12-05 19:45:44" +%s1417779944//If you know a timestamp, You can also calculate the time date for this timestamp [[email protected] shell]# Date [email protected]fri Dec 5 19:45:44 CST 2014[[email protected] shell]# date-d @1417779944fri DEC 5 19:45:44 CST 2014

Knowing that, we can count how many days away from today's Day:

#!/bin/bash#first_stamp= ' date-d "2014-12-05 19:45:44" +%s ' #计算指定日期的时间戳today_stamp = ' date +%s ' #计算 The time stamp of the day Let day_stamp= ($today _stamp-$first _stamp) #当天的时间戳减去指定的时间戳let day= ($day _stamp/86400) #相差的时 Divide the number of seconds in a day to get the number of days Echo $day

Here are some other ways to calculate the time:

[[Email protected] shell]# echo $ (date --date= ' 3 day ')   //Day date + 3 days Fri  Jan 16 11:55:02 CST 2015[[email protected] shell]# [[email  protected] shell]# echo $ (date --date= ' 3 day ago ')   //Day date-3 days sat  Jan 10 11:55:10 cst 2015[[email protected] shell]# [[email protected]  shell]# echo $ (date --date= ' 3 month ')   //Day date +3 months mon apr 13  11:55:17 cst 2015[[email protected] shell]# [[email protected] shell]#  echo $ (date --date= ' 3 month ago ')   //Day date-3 friends mon oct 13  11:55:25 cst 2014[[email protected] shell]# [[email protected] shell]#  echo $ (date --date= ' 3 year ')   //Day date + 3 years sat jan 13 11:55:32  Cst 2018[[email protected] shell]# [[email protected] shell]# echo $ (date --date= ' 3 year  ago ')   //Day date-3 years fri jan 13 11:55:38 cst 2012[[email protected]  shell]#[[email protected] shell]# echo $ (date --date= ' 3 minute  ')   //Day date + 3 seconds tue jan 13 11:58:44 cst 2015[[email protected] shell]#  [[email protected] shell]# echo $ (date --date= ' 3 minute ago ')    //Day Date-3 seconds tue jan 13 11:52:52 cst 2015[[email protected] shell]#  [[Email protected] shell]# echo $ (date --date= ' 3 hour ago ')   // Day Date-3 hours Tue jan 13 08:56:00 cst 2015[[email protected] shell]# [[email  protected] shell]# echo $ (date --date= ' 3 hour ')   //Day date + 3 hours tue  jan 13 14:56:06 cst 2015[[email protected] shell]# 

Transferred from: http://blog.51cto.com/tanxw/1602915

Subtraction operation of time and date in shell

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.