ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Since you're discarding the fractions of days, subtract 86399 seconds from it before you calculate your day count. This will round it into the next day.
Code:
echo $(( ($(date -d 'Fri Feb 05 09:55:37 GMT 2016' +%s) - $(date +%s) - 86399)/ 86400)) days
Write it in a script. Save each term value into a variable. Echo out each variable and each resultant calculation. That will help you to resolve your border cases.
What I'm saying is that I'm not convinced that the 86399 value doesn't work. Prove to me that it doesn't.
Consider that you are comparing exactly 1 day and one second's difference and you now add the 86399 value to that which is one second less than an entire day. The calculation result then becomes 2 days, which was your intentions. If you are exactly 1 day and you add 86399 you end up at one second under 2 days and the calculation result should then result in 1 day, which was your stated intentions.
The relative time difference between Feb 22 15:55:37 GMT 2016 and Feb 23 14:08:12 GMT 2016 is less than 24 hours (-0.925) so rounding down to -1 day is correct. What rtmistler posted works but not working as expected.
Not exactly sure what you are trying to accomplish but it isn't necessary to specify time when trying to find the difference between dates. Without time the default is 00:00:00.
bash doesn't do floating point and rounds down. As michaelk said, your result is less than 24 hours, which causes it to round down. Store your variables and then pass them to bc for the math part if you want the full answer.
Code:
now=$(date +%s)
then=$(date -d 'Fri Feb 22 13:55:37 GMT 2016' +%s)
diff=$(echo "($then - $now) / 86400" | bc -l)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.