Recently, when dealing with shell scripts, I encountered time processing issues. Addition and subtraction of time, and calculation of time difference.
1. Time addition and subtraction
The processing method here is to convert the base time into a timestamp, and then, it is necessary to increase or change the time to become seconds.
如:1990-01-01 01:01:01 plus 1 hour 20 minutes
Processing method:
a.Basic time Convert to timestamp
time1=$(date +%s -d '1990-01-01 01:01:01')
echo $time1
631126861 Timestamp]
b. Increase the time to seconds
[root@localhost ~]# time2=$((1*60*60+20*60 ))[root@localhost ~]# echo $time2
4800
c. Adding two times and calculating the result time
time1 =$(($time1+$time2))
time1=$(date +%Y-%m-%d\\ %H:%M:%S -d "1970-01-01 UTC $ Time1 seconds");
echo $time1
1990-01-01 02:21:01
2. Time difference calculation method
For example: 2010-01-01 and 2009-01-01 11:11:11 Time difference
Principle: Also convert to timestamp, then calculate Day, hour, minute, second
time1=$(($(date +%s -d '2010-01-01') - $(date +%s -d ' 2009-01-01 11:11:11')));
echo time1
The time1 /60 seconds will change.
Additional Note:
shell Single Bracket Operator Symbol:
a=$(date);
Equivalent to: a =`date`;
Double bracket operator:
a=$((1+2));
echo $a;< Br>
is equivalent to:
a=`expr 1 + 2`
If you are a Linux user, you may have heard that you dont need to defrag your Linux files system. Yo
1. Hard disk partition configuration: C drive: Win7 (NTFS format ——
According to statcounter statistics, the global operating system market share in March is still Win7
Regarding the circuit structure, whether it is a linear power supply, a thyristor power supply or
Briefly explain the four intrusion levels for Linux servers
There are several ways to log in to Linux on Windows platform:
Linux system Raiders: how to make quotas for added hard drives
Redhat Linux vsftpd compile and install
UNIX/LINUX Platform Executable File Format Analysis
What is the difference between Linux and BSD?
Introduction to APMD parameters and usage in Linux
Neighbor subsystem of the linux protocol stack (overview 1)
How to turn on or off the built-in positioning function in Win10
Speed up Vista file copying speed
How to deal with win7 computer card slow
Adding PHP support for IIS7 in Vista/Win2008
What should I do if I forget my password?
Tips to install Win7 PE on the hard disk
XP print sharing four major problems and solutions
WinXP computer IE prompts "This tab has been restored" What should I do?