Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date difference in months for dates #325

Closed
mvkk opened this issue Oct 29, 2015 · 10 comments
Closed

Date difference in months for dates #325

mvkk opened this issue Oct 29, 2015 · 10 comments

Comments

@mvkk
Copy link

mvkk commented Oct 29, 2015

I used the monthsBetween method to calculate months between dates and here is the summary of the outcomes:
1.'2013-03-31' and '2013-04-30' - Difference is 1
2. '2013-03-31' and '2014-02-28' - Difference is 11

While I get 0 for first and 10 for second set using SPSS, MySQL, Excel.
Is there any way we can get monthsBetween match results?

@johnjaylward
Copy link

why would those tools (SPSS, MySQL, Excel) report 0 or 10... the difference to any sane person would be what Joda time is reporting.

@AdrianAcala
Copy link
Contributor

Seems like he wants to round down.

@johnjaylward
Copy link

last day of the month to last day of the next month would be 1 month difference. I'm not seeing why you would expect a different result.

@mvkk
Copy link
Author

mvkk commented Nov 2, 2015

I guess the last day is not included. Is there an option to include or not
include last day in the calculations?
On Nov 1, 2015 9:06 PM, "johnjaylward" notifications@github.com wrote:

last day of the month to last day of the next month would be 1 month
difference. I'm not seeing why you would expect a different result.


Reply to this email directly or view it on GitHub
#325 (comment).

@AdrianAcala
Copy link
Contributor

According to Microsoft, the excel calculation is

=IF(DAY(A1)>=DAY(B1),0,-1)+(YEAR(A1)-YEAR(B1)) *12+MONTH(A1)-MONTH(B1)

If you relate both examples to this excel code, it makes sense. May has not yet passed the 31st day of the month, which would only occur on June 1st, and therefore a month has not yet passed.

@AdrianAcala
Copy link
Contributor

@mvkk, there's an issue I spotted when creating unit tests. The inverse does not make sense. For example, in your first example, 3/31/13 to 4/30/13 is suppose to have 0 months in between, but what would happen if you flipped the order so that you're looking for the months between 4/30/13 and 3/31/13. That would then be -1 months going backwards whereas 0 months going forward.

@mvkk
Copy link
Author

mvkk commented Nov 4, 2015

That is true. The reverse does not make sense. My suggestion would be to
either have a validation that alerts the users about the wrong usage of
dates or just show the negative results as done by some of the databases.

On Tue, Nov 3, 2015 at 1:05 AM, AdrianAcala notifications@github.com
wrote:

@mvkk https://github.com/mvkk, there's an issue I spotted when creating
unit tests. The inverse does not make sense. For example, in your first
example, 3/31/13 to 4/30/13 is suppose to have 0 months in between, but
what would happen if you flipped the order so that you're looking for the
months between 4/30/13 and 3/31/13. That would then be -1 months going
backwards whereas 0 months going forward.


Reply to this email directly or view it on GitHub
#325 (comment).

@jodastephen
Copy link
Member

Whether this is the right behaviour or not is slightly moot at this point. Joda-Time has worked this way for many years, so I'm not changing it.

jodastephen added a commit that referenced this issue Jan 28, 2016
Behaviour in this area is long standing and should not be changed
See #325
@mvkk
Copy link
Author

mvkk commented Jan 29, 2016

Hi Stephen,
I appreciate you letting me know of your decision.
I leave that you. I needed limited functionality to just count number of
days/months/years between two given dates.
And I have written custom functions to overcome the issues I had mentioned
in my previous emails.
Hence I am good for now.
However, if all major databases, MS Excel, statistical tools like SPSS,
STATA have implemented similar behaviour, you could think of doing the same
sometime in future.
All the best.

Regards
Kiran

On Thu, Jan 28, 2016 at 8:13 AM, Stephen Colebourne <
notifications@github.com> wrote:

Whether this is the right behaviour or not is slightly moot at this point.
Joda-Time has worked this way for many years, so I'm not changing it.


Reply to this email directly or view it on GitHub
#325 (comment).

Guardiola31337 pushed a commit to Guardiola31337/joda-time that referenced this issue Feb 8, 2016
Behaviour in this area is long standing and should not be changed
See JodaOrg#325
@jodastephen
Copy link
Member

Just to note that java.time in Java SE 8 does the following:

System.out.println(Period.between(LocalDate.of(2013, 3, 31), LocalDate.of(2013, 4, 30)));
P30D

System.out.println(Period.between(LocalDate.of(2013, 3, 31), LocalDate.of(2014, 2, 28)));
P10M28D

As stated above however, I won't be updating Joda-Time (which is primarily in maintenance mode).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants