Currently, I am calculating the difference between two dates in months using this code:
_result = Math.abs(moment(date1).diff(moment(date2), 'months')) + 1;
This code returns an integer representing the duration in number of months. Now, I would like to change the output format to display exactly as follows:
2 years, 3 months
If the year or month value is 1, then the format should be:
1 year, 1 month
How can I modify my code to achieve this desired result?
NOTE: I currently have the value in months, but I can easily convert it to days or years. However, the challenge lies in formatting the output to include both years and months as mentioned above.