I’m trying to generate a “changefreq” for an xml sitemap. Each time I save a page I add a date to a “save_history” Array which gives me a list of dates to work with. Initially I thought I would just add up all the dates and divide the the length but that just gives me the average time since 1/1/1970. How can I fix this function to get the average time between the dates?
http://jsfiddle.net/jwerre/pAfdM/19/
or
getChangeFequency = function(history) {
var sum = _.reduce(history, function(memo, num) {
return memo + num.getTime();
}, 0);
var average = sum / history.length;
var hours = average / 3600000;
console.log("totals:", sum, average, hours); // 20292433147523 1352828876501.5334 375785.7990282037
if (hours > 17532) {
return "never";
} else if ((8766 < hours && hours > 17531)) {
return "yearly";
} else if ((730 < hours && hours > 8765)) {
return "monthly";
} else if ((168 < hours && hours > 729)) {
return "weekly";
} else if ((24 < hours && hours > 167)) {
return "daily";
} else if ((1 < hours && hours > 23)) {
return "hourly";
} else {
return "always";
}
};
save_history = [ Tue Nov 13 2012 09:47:39 GMT-0800 (PST), Tue Nov 13 2012 09:47:44 GMT-0800 (PST), Tue Nov 13 2012 09:47:45 GMT-0800 (PST), Tue Nov 13 2012 09:47:46 GMT-0800 (PST), Tue Nov 13 2012 09:47:47 GMT-0800 (PST) ]
getChangeFrequency(save_history)
As your history is a sorted array of dates, the average timespan can be computed easily:
This is mathematically equivalent to building an array of intervals and averaging them. The result is in milliseconds.