How to convert a number of months, into months and years? [closed]

If I have X months, using Javascript, how could I convert that number into X years and X months?

Answer

You could use a function for it with stress on singular and plural words.

function getWords(monthCount) {
    function getPlural(number, word) {
        return number === 1 && word.one || word.other;
    }

    var months = { one: 'month', other: 'months' },
        years = { one: 'year', other: 'years' },
        m = monthCount % 12,
        y = Math.floor(monthCount / 12),
        result = [];

    y && result.push(y + ' ' + getPlural(y, years));
    m && result.push(m + ' ' + getPlural(m, months));
    return result.join(' and ');
}

var i;
for (i = 0; i < 30; i++) {
    console.log(getWords(i));
}