How to find the difference in days between two dates?
If you have GNU date
, it allows to print the representation of an arbitrary date (-d
option).
In this case convert the dates to seconds since EPOCH, subtract and divide by 24*3600.
Example using GNU date (from https://stackoverflow.com/a/9008871/215713):
let DIFF=($(date +%s -d 20210131)-$(date +%s -d 20210101))/86400
echo $DIFF
30
This also works with dates in other formats, for example "2021-01-31".
Other answers suggest ways to do it that don't require GNU date.
How do I get the difference between two dates under bash
let DIFF=(`date +%s -d 20120203`-`date +%s -d 20120115`)/86400
echo $DIFF
Shell script to get difference between two dates
Using only date and shell arithmetics:
echo $((($(date -d "2010-06-01" "+%s") - $(date -d "2010-05-15" "+%s")) / 86400))
Find difference between two dates in bash
You can use:
date1="Sat Dec 28 03:22:19 2013"
date2="Sun Dec 29 02:22:19 2013"
date -d @$(( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) )) -u +'%H:%M:%S'
And the output is:
23:00:00
However, for arbitrary differences over 24 hours, you have to start worrying about how the value for days will be represented. Negative values will also present problems.
The problem with this is that if you drop the format string, the date presented is:
Thu Jan 1 23:00:00 UTC 1970
So, while this works for the two given date/time values, it is not really a general solution.
Consider adding %j
for the day of year; it will work semi-sanely for up to 1 year's difference.
Otherwise, capture the difference in a variable and present the results with a separate calculation.
date1="Sat Dec 28 03:22:19 2013"
date2="Sun Dec 29 02:22:19 2013"
delta=$(( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) ))
if [[ $delta -lt 0 ]]
then sign="-"; delta=${delta/^-/}
else sign="+"
fi
ss=$(( $delta % 60 ))
delta=$(( $delta / 60 ))
mm=$(( $delta % 60 ))
delta=$(( delta / 60 ))
hh=$(( $delta % 24 ))
dd=$(( $delta / 24 ))
printf "$sign%d %.2d:%.2d:%.2d\n" $dd $hh $mm $ss
Output:
+0 23:00:00
(Meaning: a positive difference of 0 days, 23 hours, 0 minutes, 0 seconds.) Splitting a number of days such as 1000 into years, months and days is fraught without any reference dates. Here, there are reference dates to help, but it would still be ... fun.
bash difference between 2 dates in bash by yyyy-mm-dd in N days
Expanding on my comment:
see the accepted answer for Quickly calculate date differences
The person who answered that question wrote a custom function called datediff
:
$ datediff() {
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d1 - d2) / 86400 )) days
}
We can use this same function to compare the 2 dates from the question:
$ START=$(date --date=yesterday +%F)
$ END=$(date --date "2 days ago" +%F)
$ echo "${START} : ${END}"
2019-12-30 : 2019-12-29
$ datediff "${START}" "${END}"
1 days
Calculate difference in months between two dates in Unix?
How about:
#!/bin/bash
DATE1=201712
DATE2=201801
y1=${DATE1:0:4}
m1=${DATE1:4:2}
y2=${DATE2:0:4}
m2=${DATE2:4:2}
diff=$(( ($y2 - $y1) * 12 + (10#$m2 - 10#$m1) ))
echo $diff
Time difference in seconds between given two dates
Assuming GNU implementation based OS, you can use date
's option %s
and -d
to calculate the time difference in seconds
using command substitution and arithmetic operations.
START="2019-01-05 23:52:00"
END="2019-01-06 00:02:10"
Time_diff_in_secs=$(($(date -d "$END" +%s) - $(date -d "$START" +%s)))
echo $Time_diff_in_secs
Output:
610
Hope this helps!!!
Related Topics
Error on Execution -Version 'Qt_5' Not Found Required By
How to Script a "Yes" Response for Installing Programs
How to Extract Only the Raw Contents of an Elf Section
How to Remove the Bom from a Utf-8 File
Vim Configuration for Linux Kernel Development
Using 'Date' Command to Get Previous, Current and Next Month
Where Does Eclipse Look for Eclipse.Ini Under Linux
Write a Bash Shell Script That Consumes a Constant Amount of Ram for a User Defined Time
How to Set Cron to Display Gui Application
Command Line: Search and Replace in All Filenames Matched by Grep
When Should I Use Tcp_Nodelay and When Tcp_Cork
Replacing Control Character in Sed
Linux Terminal Input: Reading User Input from Terminal Truncating Lines at 4095 Character Limit
Send Mail from Linux Terminal in One Line
How to Get the Percentage of Memory Free with a Linux Command
Why No Output Is Shown When Using Grep Twice
How to Force Abort on "Glibc Detected *** Free(): Invalid Pointer"