admin管理员组

文章数量:1425774

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?

/

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)

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/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)
Share Improve this question edited Nov 13, 2012 at 19:37 Mike Brant 71.4k10 gold badges101 silver badges104 bronze badges asked Nov 13, 2012 at 19:22 PardonerPardoner 1,1214 gold badges16 silver badges28 bronze badges 2
  • Yes, I'm trying to get the average hours between changes. – Pardoner Commented Nov 13, 2012 at 19:28
  • You should calculate the differences between those hours (subtract 1 from 2, 2 from 3, etc) and calculate that average. – Mike Robinson Commented Nov 13, 2012 at 19:34
Add a ment  | 

3 Answers 3

Reset to default 5

How can I fix this function to get the average time between the dates?

As your history is a sorted array of dates, the average timespan can be puted easily:

(_.last(history) - history[0]) / (history.length - 1)

This is mathematically equivalent to building an array of intervals and averaging them. The result is in milliseconds.

Build an array of intervals. So assuming that history is sorted from earliest to latest date of change, it could look like this

var intervals = [];
for (i = 0; i < history.length - 1; i++) {
  intervals[i] = history[i+1].getTime() - history[i].getTime();
}

var sum = _.reduce(intervals, function(memo, num) {
  return memo + num;
}, 0);

var average = sum / intervals.length;
var hours = average / 3600000;

How can I fix this function to get the average time between the dates?

  1. From your existing data set, generate a new array which is the time difference between each date in the original data.
  2. Using this newly generated data set, find the average value.
  3. Calculate the time value relative to midnight 1970. That is, if the calculated average is January 2, 1970 at 2:03 PM, the difference is 1 day, 14 hours, and 3 minutes.

本文标签: javascriptHow do I get the average time from an Array of datesStack Overflow