How do I get the difference between two dates unde

2020-08-09 07:34发布

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?

标签: linux bash date
3条回答
beautiful°
2楼-- · 2020-08-09 08:02

How I solved this is: (hope this eventually contains some more useful stuff)

current="$(date +%s.%N)" #current date, precise to nanoseconds
old="$(date +%s.%N -d "$(sh some_script_that_gives_a_date.sh)")" #convert to ns too
diff=$(echo "$current-$olc" |bc)

date +%s.%N -d $1 takes an arbitrary date and converts it to the given format.


To convert it back to human-readable, you can use

date $2 -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/

查看更多
做个烂人
3楼-- · 2020-08-09 08:11

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).

function gendates { for dd in $(seq -w $1 $2) ; 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:

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.

And if you happen to have MySQl or similar installed there is a very quick option :

mysql -BNe "SELECT DATEDIFF($1,$2) 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)

[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.

查看更多
女痞
4楼-- · 2020-08-09 08:21
let DIFF=(`date +%s -d 20120203`-`date +%s -d 20120115`)/86400
echo $DIFF
查看更多
登录 后发表回答