'How to Change "%d years ago" so that it displays like this "2 years and 4 months ago"

i want to Change "%d years ago" so that it displays like this "2 years and 4 months ago"

(function timeAgo(selector) { var templates = {prefix: "", suffix: " ", seconds: "Just Now", minute: "Just Now", minutes: "%d min ago", hour: "An hour ago", hours: "%d hours ago", day: "Yesterday", days: "%d days ago", week: "A week ago", weeks: "%d weeks ago", month: "A month ago", months: "%d months ago", year: "A year ago", years: "%d years ago"}; var template = function(t, n) { return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n))); }; var timer = function(time) { if (!time) return; time = time.replace(/\.\d+/, ""); time = time.replace(/-/, "/").replace(/-/, "/"); time = time.replace(/T/, " ").replace(/Z/, " UTC"); time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); time = new Date(time * 1000 || time); var now = new Date(); var seconds = ((now.getTime() - time) * .001) >> 0; var minutes = seconds / 60; var hours = minutes / 60; var days = hours / 24; var weeks = days / 7; var months = weeks / 4; var years = months / 12; return templates.prefix + ( seconds < 45 && template('seconds', seconds) || seconds < 90 && template('minute', 1) || minutes < 45 && template('minutes', minutes) || minutes < 90 && template('hour', 1) || hours < 20 && template('hours', hours) || hours < 42 && template('day', 1) || days < 7 && template('days', days)|| days < 12 && template('week', 1) || weeks < 4 && template('weeks', weeks)|| weeks < 6 && template('month', 1) || months < 12 && template('months', days / 30) || years < 1.5 && template('year', 1) || template('years', years) ) + templates.suffix; }; var elements = document.getElementsByClassName('dtTm'); for (var i in elements) { var $this = elements[i]; if (typeof $this === 'object') { $this.innerHTML = timer($this.getAttribute('title') || $this.getAttribute('data-datetime')); } } setTimeout(timeAgo, 60000); })


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source