fingerpich / jalali-moment

Display, parse, manipulate, validate and convert jalali (Persian, Khorshidi, Shamsi) or Gregorian (Miladi) dates and times.
https://fingerpich.github.io/jalali-moment/
MIT License
436 stars 68 forks source link

Moment Difference returns wrong number if locale is set to 'fa' #78

Closed ahmadx87 closed 4 years ago

ahmadx87 commented 5 years ago

Describe the bug The package returns wrong number when you want to find the differences (day, month, year, ...) if the locale is set to 'fa'. moment('2019-10-26').locale('fa').diff(moment('2019-10-28').locale('fa'), 'month') // 108!! moment('2019-10-26').locale('en').diff(moment('2019-10-28').locale('en'), 'month') // 0

To Reproduce see demo

fingerpich commented 4 years ago

I added a test and it worked, but why the demo is wrong? can you check the demo?

ahmadx87 commented 4 years ago

Thank for your input. The fiddle is pretty simple. I don't know how the tests are passing but the demo result is wrong. I even tested it again in a node environment but the result is the same. I get wrong numbers if locale is fa.

fingerpich commented 4 years ago

can you please check the code and find the problem?

fingerpich commented 4 years ago

Can you please check the 3.3.5 and the commits? edited demo

ahmadsdn commented 4 years ago

Sorry I didn't get a chance to get at it sooner. I took a look at the commit. I think there are a few inconsistencies. According to the moment documentation :

If the moment is earlier than the moment you are passing to moment.fn.diff, the return value will be negative.

But in here the absolute value is returned. Another issue is that only the month difference is checked. What if the years are different? for example 2020.05.30 and 2021.05.30, the month difference would be 0 but with locale 'en' it is 12.

fingerpich commented 4 years ago

good points. thank you. can you please create a PR?

ahmadsdn commented 4 years ago

Sure, I'll work on that.

ahmadsdn commented 4 years ago

I created this pull request https://github.com/fingerpich/jalali-moment/pull/96 I borrowed code from the original moment diff method. The tests are passing. Hope it resolves the issue.

fingerpich commented 4 years ago

please check v3.3.7

ahmadsdn commented 4 years ago

I did several other tests in the browser and it looks fine. Thanks!