尝试这样的东西
function humanise (diff) {
// The string we're working with to create the representation
var str = '';
// Map lengths of `diff` to different time periods
var values = [[' year',365],[' month',30],[' day',1]];
// Iterate over the values...
for (var i=0;i
var amount = Math.floor(diff / values[i][1]);
// ... and find the largest time value that fits into the diff
if (amount >= 1) {
// If we match,add to the string ('s' is for pluralization)
str += amount + values[i][0] + (amount > 1 ? 's' : '') + ' ';
// and subtract from the diff
diff -= amount * values[i][1];
}
}
return str;
}
预计这个论点是你想代表的日子差异.它假设一个30天,一年365.
你应该这样使用它
$('#savings-goal span').text(humanise(timeToGoal));