我正在尝试为 xml 站点地图生成“changefreq”。每次我保存一个页面时,我都会在“save_history”数组中添加一个日期,它会为我提供一个可以使用的日期列表。最初我以为我只需将所有日期相加并除以长度,但这只是给了我自 1970 年 1 月 1 日以来的平均时间。如何修复此功能以获取日期之间的平均时间?
http://jsfiddle.net/jwerre/pAfdM/19/
或者
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)