LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2016, 07:28 AM   #1
sweeny_here
LQ Newbie
 
Registered: Feb 2016
Posts: 21

Rep: Reputation: Disabled
Count Number of Days


Hello - I wish to count the number of day’s between two dates.

This can be achieved by using -

$ date
Tue Feb 23 13:24:42 GMT 2016

echo $(( ($(date -d 'Fri Feb 05 09:55:37 GMT 2016' +%s) - $(date +%s) )/ 86400)) days

Result -

-18 days

But I'm seeking a result of 19 days, where the current partial day counts as 1 full day, producing 19 days.

Any ideas?
 
Old 02-23-2016, 07:46 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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

Last edited by rtmistler; 02-23-2016 at 07:49 AM.
 
Old 02-23-2016, 08:00 AM   #3
sweeny_here
LQ Newbie
 
Registered: Feb 2016
Posts: 21

Original Poster
Rep: Reputation: Disabled
I tried this hack but it still fails on the second instance - where it give 1 day, instead the result sought is 2 days.

$ date
Tue Feb 23 13:24:42 GMT 2016

$ mydate=$(( ($(date +%s) - $(date -d 'Mon Feb 22 09:55:37 GMT 2016' +%s) )/ 86400)) && mydate=$((mydate+1)) && printf "%s days", $mydate
2 days

$ mydate=$(( ($(date +%s) - $(date -d 'Mon Feb 22 19:55:37 GMT 2016' +%s) )/ 86400)) && mydate=$((mydate+1)) && printf "%s days", $mydate
1 days
 
Old 02-23-2016, 08:20 AM   #4
sweeny_here
LQ Newbie
 
Registered: Feb 2016
Posts: 21

Original Poster
Rep: Reputation: Disabled
So I did some further testing -

$ date
Tue Feb 23 14:08:12 GMT 2016

This example works -

$ echo $(( ($(date -d 'Fri Feb 22 13:55:37 GMT 2016' +%s) - $(date +%s) - 86399)/ 86400)) days
-2 days

But this fails - the result sought here is still two days, but 1 day is produced.

$ echo $(( ($(date -d 'Fri Feb 22 15:55:37 GMT 2016' +%s) - $(date +%s) - 86399)/ 86400)) days
-1 days

What is being sought - 1 day = round up each day, even if only 1 second past midnight.
 
Old 02-23-2016, 08:24 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You should type "set -xv" in your terminal window so it can print out some debug to show you what all the calculations are doing.
 
Old 02-23-2016, 08:58 AM   #6
sweeny_here
LQ Newbie
 
Registered: Feb 2016
Posts: 21

Original Poster
Rep: Reputation: Disabled
Debug is not giving sufficient insight into the inner workings -

$ echo $(( ($(date -d 'Fri Feb 22 15:55:37 GMT 2016' +%s) - $(date +%s) - 86399)/ 86400)) days
echo $(( ($(date -d 'Fri Feb 22 15:55:37 GMT 2016' +%s) - $(date +%s) - 86399)/ 86400)) days
date -d 'Fri Feb 22 15:55:37 GMT 2016' +%s
++ date -d 'Fri Feb 22 15:55:37 GMT 2016' +%s
date +%s
++ date +%s
+ echo -1 days
-1 days
 
Old 02-23-2016, 09:07 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 02-23-2016, 11:06 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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.

Last edited by michaelk; 02-23-2016 at 12:02 PM.
 
Old 02-23-2016, 12:08 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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)
 
Old 02-23-2016, 12:59 PM   #10
RedDog2
LQ Newbie
 
Registered: Feb 2016
Location: SoCal
Distribution: SLES 11/SP3 Server
Posts: 5

Rep: Reputation: Disabled
When I've been working with comparing dates, it seems using the epoch date (date +%s) is the only way to get what I've been looking for.

From the man page for date:

%s seconds since 1970-01-01 00:00:00 UTC

Example:

# date +%s
1456253803
#


Then we can do the math however we like.
 
Old 02-24-2016, 03:22 AM   #11
sweeny_here
LQ Newbie
 
Registered: Feb 2016
Posts: 21

Original Poster
Rep: Reputation: Disabled
The following has done the trick -

$ now=$(date +%s) && then=$(date -d 'Tue Feb 22 13:55:37 GMT 2016' +%s) && count=$(echo "($then - $now) / 86400" | bc -l) && echo $count |awk -F'-' '{printf "%0.f\n", $2}'
2

now=$(date +%s) && then=$(date -d 'Tue Feb 23 13:55:37 GMT 2016' +%s) && count=$(echo "($then - $now) / 86400" | bc -l) && echo $count |awk -F'-' '{printf "%0.f\n", $2}'
1

now=$(date +%s) && then=$(date -d 'Wed Feb 24 13:55:37 GMT 2016' +%s) && count=$(echo "($then - $now) / 86400" | bc -l) && echo $count |awk -F'-' '{printf "%0.f\n", $2}'
0
 
Old 02-24-2016, 03:48 AM   #12
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Would appear to be a perfect use-case for dateutils.
 
1 members found this post helpful.
Old 02-24-2016, 08:19 AM   #13
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Why are you subtracting it in the wrong direction and then using awk to remove the negative sign? Just subtract it in the right order.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to count number sessions of users? wwinay Linux - General 11 02-28-2013 02:40 AM
Count characters and list down the number bison72 Linux - Newbie 3 01-22-2013 10:34 AM
[SOLVED] PHP: Count number of value in sub array angel115 Programming 2 08-26-2011 03:35 AM
[SOLVED] count how many number occurrences in series kpinto Linux - Newbie 4 06-02-2011 08:22 AM
count number of used ports mnchur Linux - General 2 02-10-2009 02:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration