【问题标题】:how can I calculate frequency of visits for a web page如何计算网页的访问频率
【发布时间】:2020-06-18 21:29:50
【问题描述】:

我需要计算一个网页的访问频率,我正在使用 ExpressJS、mongoDB 和 PugJS

假设我将每个访问 datetimestamp 存储在数据库中的数组中,如下所示:

  • 第一次:2020年1月1日12:00:00
  • 第二次:2020-01-03 15:00:00
  • 第三次:2020-01-06 15:00:00
  • 第四次:2020-01-08 22:00:00

然后应该说

访问频率:“每 7 分钟”或“每 7 天”或“每 2 个月”或“每 3 天”......等等。

我该怎么做?请指教。

【问题讨论】:

  • 您是否将每个访问日期存储在一个数组中?
  • @sonEtLumiere 是的

标签: javascript express time momentjs


【解决方案1】:

你可以用一点数学来做到这一点。只需计算时间戳之间差异的平均值。一个例子:

const visits = [1592512909123, 1592512910123, 1592512901223, 1592512912753];

let average = 0;
for (let i = 1; i < visits.length; i++){
  average += visits[i] - visits[i-1];
}
average /= visits.length - 1;

console.log(`The site gets visited every ${average / 1000} seconds.`);

然后您可以将平均值转换为秒/分钟/小时/天。

【讨论】:

    【解决方案2】:

    我希望这会有所帮助。您可以获取以毫秒为单位的日期之间的差异,然后传递一个函数将毫秒转换为秒、分钟、小时或年。

    var dates = ["Jun 01 2020 12:00:00", "Jun 03 2020 15:00:00", "Jun 06 2020 15:00:00", "Jun 08 2020 22:00:00"];
    var fechas = [];
    for(i=0; i<dates.length; i++){
    	fechas.push(new Date(dates[i]));
    }
    console.log(fechas);
    
    function convertMiliseconds(miliseconds, format) {
      var days, hours, minutes, seconds, total_hours, total_minutes, total_seconds;
      
      total_seconds = parseInt(Math.floor(miliseconds / 1000));
      total_minutes = parseInt(Math.floor(total_seconds / 60));
      total_hours = parseInt(Math.floor(total_minutes / 60));
      days = parseInt(Math.floor(total_hours / 24));
    
      seconds = parseInt(total_seconds % 60);
      minutes = parseInt(total_minutes % 60);
      hours = parseInt(total_hours % 24);
      
      switch(format) {
    	case 's':
    		return total_seconds;
    	case 'm':
    		return total_minutes;
    	case 'h':
    		return total_hours;
    	case 'd':
    		return days;
    	default:
    		return { d: days, h: hours, m: minutes, s: seconds };
      }
    };
    
    var milisecondsDiference = fechas[1] - fechas[0];
    console.log(convertMiliseconds(milisecondsDiference, 'h'));		//Output 51 hours

    【讨论】:

      猜你喜欢
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多