Skip to content

Instantly share code, notes, and snippets.

@adickson311
Last active December 22, 2015 06:19
Show Gist options
  • Save adickson311/6430321 to your computer and use it in GitHub Desktop.
Save adickson311/6430321 to your computer and use it in GitHub Desktop.
This is a jQuery utility method I wrote that takes a number representing the number of months and returns a string representation of that number in years and months.
$.monthsToYears = function(months){
var val;
if(months < 12) {
val = months + ' month';
if(months > 1 || months === 0){
val += 's';
}
} else {
var yrs = Math.floor(months/12);
var mo = months%12;
val = yrs + ' year';
if(yrs > 1){
val += 's';
}
if(mo != 0){
val += ' ' + mo + ' month';
}
if(mo > 1){
val += 's';
}
}
return val;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment