简体   繁体   English

如何从一组日期中获取平均时间?

[英]How do I get the average time from an Array of dates?

I'm trying to generate a "changefreq" for an xml sitemap. 我正在尝试为xml网站地图生成“ changefreq”。 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. 每次保存页面时,都会在“ save_history”数组中添加一个日期,该数组会为我提供要使用的日期列表。 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. 最初,我以为我只想将所有日期加起来并除以长度即可,但这仅是自1970年1月1日以来的平均时间。 How can I fix this function to get the average time between the dates? 如何修复此函数以获得日期之间的平均时间?

http://jsfiddle.net/jwerre/pAfdM/19/ 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)

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 computed 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 因此,假设history是从最早的更改日期到最新的更改日期,则它看起来可能像这样

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. 计算相对于1970午夜的时间值。也就是说,如果计算的平均值是1970年1月2日下午2:03,则相差1天14小时3分钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM