Linux 如何在bash下获得两个日期之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9008824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the difference between two dates under bash
提问by owagh
Exactly as the question sounds. I want to subtract say 20120115 from 20120203 and get 19 as the answer. What is the best way to implement this in a shell script?
就像问题听起来一样。我想从 20120203 中减去 20120115 并得到 19 作为答案。在 shell 脚本中实现它的最佳方法是什么?
采纳答案by Eugen Rieck
let DIFF=(`date +%s -d 20120203`-`date +%s -d 20120115`)/86400
echo $DIFF
回答by runlevel0
A quick and utterly dirty fix that can be used in scripts (quick and also fast):
可以在脚本中使用的快速且完全肮脏的修复(快速且快速):
This function gendates
generates a list of valid dates between two dates in YYYYmmdd format (oldest first).
此函数gendates
以 YYYYmmdd 格式(最早的在前)生成两个日期之间的有效日期列表。
function gendates { for dd in $(seq -w ) ; do date -d $dd +%Y%m%d 2>/dev/null ; done ; }
I use this function for several purposes related to log files, like generating a list of logfile names to check [1]. And it comes handy to count the difference in days too:
我将此函数用于与日志文件相关的多个目的,例如生成要检查的日志文件名称列表 [1]。计算天数差异也很方便:
echo "$(gendates YYYYmmdd YYYYmmdd)" | wc -l
YYYYmmdd have to be dates, of course. It only works if $1 is an earlier date than $2 and it's slow for large date differences, but for a period of a few years and to be used in ad-hoc scripting it's quite handy.
当然,YYYYmmdd 必须是日期。它仅在 $1 比 $2 更早的日期时才有效,并且对于大的日期差异很慢,但是在几年的时间内并用于临时脚本,它非常方便。
And if you happen to have MySQl or similar installed there is a very quick option :
如果您碰巧安装了 MySQl 或类似软件,则有一个非常快速的选项:
mysql -BNe "SELECT DATEDIFF(,) AS DiffDate ;" | tr -d -
The last tr
allows you to enter the dates in any order (MySQL would else render a '-'if the first date is earlier than the second)
最后一个tr
允许您以任何顺序输入日期(如果第一个日期早于第二个日期,MySQL 将呈现“-”)
[1] The reason for generating a list of dates is that with that I can generate the names of the logfiles that are in this format: YYYYmmdd.log.gz. I could do that using an asterisk or $(ls), but this is way slower than just providing a list with strings.
[1] 生成日期列表的原因是我可以生成这种格式的日志文件的名称:YYYYmmdd.log.gz。我可以使用星号或 $(ls) 来做到这一点,但这比仅提供带有字符串的列表要慢得多。
回答by Cadoiz
How I solved this is: (hope this eventually contains some more useful stuff)
我是如何解决这个问题的:(希望这最终包含一些更有用的东西)
current="$(date +%s.%N)" #current date precise to ns
old="$(date +%s.%N -d "$(sh some_script_that_gives_a_date.sh)")" #convert to seconds too
diff=$(echo "$current-$olc" |bc)
date +%s.%N -d $1
takes an arbitrary date and converts it to the given format.
date +%s.%N -d $1
获取任意日期并将其转换为给定格式。
要将其转换回人类可读的,您可以使用
date -d @0$diff #Pad diff with leading zero
Where $2 is a date format see https://www.tutorialkart.com/bash-shell-scripting/bash-date-format-options-examples/
其中 $2 是日期格式,请参阅https://www.tutorialkart.com/bash-shell-scripting/bash-date-format-options-examples/