Last active
December 22, 2015 06:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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