Getting number of months and years between two moments

Hi,

I managed to get days and weeks between two moments. Nevertheless, I realized that it is much more complicated to get the exact number of months or even years between two moments. Do you have a code snippet to get these?

If I would implement it from scratch I would do it like this:
1. get the year of both moments (lets assume that moment2 is greater than moment1)
2. get the diff between the years:
var year1 = Greg.info(moment1, Date.FORMAT_SHORT)).year;
var year2 = Greg.info(moment2, Date.FORMAT_SHORT)).year;
yearDiff = year2-year1;

3. create a new moment by adding one year to moment1:
moment3 = moment1.add(yearDiff*31449600);
4. compare moment3 with moment2:
if (moment2.greaterThan(moment3) and yearDiff > 0) {
yearDiff -= 1;
}


Nevertheless step 3 does not take leap years into account. Is there a more intelligent and efficient way to get the number of years?

The problem get's even worth for calculating months.....
  • have you considered not using the moments, but the month/day/year for moment1 and moment2 and doing the math based on those? Getting years and months between the two would be easy, and leap years wouldn't really have any impact.

    Think of it with this example: How many years and months is it between the day you were born and today? I can tell you with very simple math (not even a calculator required) that its (years redacted!), 1 month, 17 days, and have no idea how many leap years happened during those years..


    If you wanted to make moment3 1 year after moment1, you just use the same month/day, but add 1 to the year (a check for Feb 29th in moment1 might be needed so you don't try to create a moment with year=2017, month=Feb and day=29 as I'm not sure how mc would handle that)
  • Hi Jim,

    How would these simple maths look like? What I want to do is a widget called Age which does exactly what you described: calculating several formats of durations, e. g.
    y, m, w, d
    m, w, d
    w, d
    d

    Probably you mean following: let's assume a date1 23/4/2015 and date2 21/3/2016

    I would get following age formats:
    0, 10, 4, 2
    10, 4, 2
    47, 5
    334


    Imo you have to do the algorithm I described to get to the number of years (regardless of using moments or other formats). Am I right?

    Even more complicated is the calculation of the format 'w, d' since a month has more than 4 weeks. My first thought was to get that format by doing a subtraction of two moments giving me a duration which I can easily format in weeks and days by doing simple divisions.

    Additionally, (Imo) the leap years are not negligible by doing that calculations right. But this problem is a minor one for now.
  • Hi Jim,

    I followed your suggestion and used year/month/day format instead of moment. I did some "medium"-simple (ha) math to build the widget "ages" which goes live after Garmin's review here: https://apps.garmin.com/de-DE/apps/09a61b2a-2662-4536-8640-85dc575ff11b

    Thanks for your tip.