Linux computing time difference in order to maintain computing, you need to know the time difference between multiple slave nodes to obtain different time points. The following code does not check the input value and the current value in the same month. However, if we boast the month's backtracking data, there will be a big problem. # Date variables forif [-n "$1"]; then TODAY = 'date-d "" + "% Y % m % d" 'base = $ ($ TODAY-$1 )) elif [-z "$ {DAYAGO1}"]; then base = 0 fiarr = ($ base $ ($ base + 1) $ ($ base + 2 )) $ ($ base + 8) $ ($ base + 31) dag = (0 1 2 8 31) for I in {0 .. 4 }; do echo $ I echo $ {dag [$ I]} echo $ {arr [$ I]} export "DAYAGO $ {dag [$ I]}" = 'date-d" $ {arr [$ I]} days ago "+" % Y % m % d "'done to fix bugs, please refer to the following article for more information: define a solution that almost works: use the % s date format of GNU date, which prints the number of seconds since. these can be subtracted to find the time difference between two dates. echo $ (date-d 2010-06-01 + % s)-$ (date-d 2010-05-15 + % s)/86400) But the following displays 0 in some locations: echo $ (date-d 2010-03-29 + % s)-$ (date-d 2010-03-28 + % s)/86400) Because of daylight savings time, there are only 23 hours between those times. you need to add at least one hour (and at most 23) to be safe. echo $ (date-d 2010-03-29 + % s)-$ (date-d 2010-03-28 + % s) + 43200)/86400 )) or you can tell date to work in a timezone without DST. echo $ (date-u-d 2010-03-29 + % s)-$ (date-u-d 2010-03-28 + % s)/86400 )) (POSIX says to call the reference timezone is UTC, but it also says not to count leap seconds, so the number of seconds in a day is always exactly 86400 in a GMT + xx timezone .) the final result is almost perfect: # Date variables forif [-n "$1"]; then TODAY = 'date-d "" + "% Y % m % d" 'backday = $1 base = $ (date-d $ TODAY + % s) -$ (date-d $ BACKDAY + % s) + 43200)/86400) elif [-z "$ {DAYAGO1}"]; then base = 1 fiarr = ($ base) $ ($ base + 1) $ ($ base + 7 )) $ ($ base + 30) dag = (1 2 8 31) for I in {0 .. 3 }; do echo $ {dag [$ I]} echo $ {arr [$ I]} export "DAYAGO $ {dag [$ I]}" = 'date-d "$ {arr [$ I]} days ago "+" % Y % m % d "'done shell converts the time calculation to the second for the subtraction operation NOW = 'date +" % Y-% m- % d % H: % M: % S "'lastline =$ (ls-lt *" $ v_DIRNAME "| line | awk '{print $6, $7, $8 }') # obtain the file's last time: 14:30:00 Sys_data = 'date-d "$ CURTIME" + % s' # convert the current time to Linux time In_data = 'date-d "$ LASTLINE" + % s 'interval = 'expr $ Sys_data-$ In_data '# calculate the difference of two time periods