You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using moment for some date duration validation, and got a failing test around a month border. We are trying to get the months difference, and a later date is showing a smaller difference. Is the months difference something we shouldn't rely on, or is this a bug, or expected behavior due to the annoying month of February?
// notice how the diff for 01-Feb to 01-May goes DOWN from 30-Apr. It goes back up moment('2014-02-01').diff(moment('2014-04-30 23:59:59'),'months',true);// -3.034482359514687moment('2014-02-01').diff(moment('2014-05-01 23:59:59'),'months',true);// -3.0338979127432517moment('2014-02-01').diff(moment('2014-05-02 23:59:59'),'months',true);// -3.0677962178279974
Doing the same without times results in -3 for two different dates:
This is happening because while adjusting days, to convert the days difference into months it takes the average of number of days in both the months involved (i.e Feb and April in first case , Feb and May in second case). Since may has more days than April, one day in the second case weighs less than one day in the first case and that is why it decreases.
I guess the decision to use this implementation was taken after a lot of discussion: #571
Yeah, I went reading the code to see if I could figure it out. I saw the thing about average days too, and figured that might be what was causing this. After some quiet reflection, I don't see how a unit called "months" can ever be accurate, or even what accurate means in this case. The best you can do is have defined behavior. What moment.js has now is about as good as it can get, I think.
I think the only alternative is for me to define "1 month" as being from DD/MM to DD/(MM+1) and see if that satisfies 'em.
I am using moment for some date duration validation, and got a failing test around a month border. We are trying to get the
months
difference, and a later date is showing a smaller difference. Is the months difference something we shouldn't rely on, or is this a bug, or expected behavior due to the annoying month of February?Doing the same without times results in -3 for two different dates:
Edit: Downloaded
moment.min.js
yesterday,v2.8.4
The text was updated successfully, but these errors were encountered: