【问题标题】:Sum Data based on Time for weekly,monthly,yearly using javascript使用javascript根据每周、每月、每年的时间求和数据
【发布时间】:2015-08-05 07:41:31
【问题描述】:

我有一个使用 AJAX 的 JSON js 对象。 问题是我对后端没有太多控制权,所以数据来自简单的 JS 对象。

我有一个如下的 JSON 文件

var test =
[
{value : 23,
file_date : Wed Jan 01 2014 05:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 01 2014 09:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 01 2014 02:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 02 2014 06:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 02 2014 09:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 02 2014 04:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 03 2014 02:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 03 2014 01:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 03 2014 10:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 04 2014 7:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 05 2014 10:34:53 GMT+0530 (IST)
},
{value : 23,
file_date : Wed Jan 06 2014 11:34:53 GMT+0530 (IST)
}
]

file_ts 有不同的时间戳。我的问题是我必须整合这些数据。

如果用户选择 1 天,那么我必须按小时获取所有数据的总和。如果 2 到 3 小时之间有 4 个值,那么我必须按小时对这些数据求和。

然后如果每周然后每周。我正在尝试使用 express 和 mysql 编写 nodejs 服务器,但它也无法从那里工作。

【问题讨论】:

  • 向我们展示您的尝试,这与其说是一个问题,不如说是一个工作规范。
  • 我无法为此实现任何东西。我的解决方案是不同的。这刚刚出现,因为后端不在我的控制之下。我很难过

标签: javascript jquery node.js date momentjs


【解决方案1】:

这就是我假设你所要求的:

如果用户选择“1 月 1 日”,您希望获取所有信息 file_date 在选定的日期,并将所有小时数相加。

以下是您需要采取的步骤:

  • 从 Ajax 调用中获取数据
  • 按日期(输入)搜索数据并添加匹配项
  • 将匹配项转换为日期并累加小时数

注意:我已将您的时区 (IST) 转换为 (UTC),因此输出将以 GMT 为单位

// Test data
var test = [{  
    value: 23,
    file_date: "Wed Jan 01 2014 05:34:53 GMT+0530 (IST)"
  },{  
    value: 23,
    file_date: "Wed Jan 01 2014 09:34:53 GMT+0530 (IST)"
  },{  
    value: 23,
    file_date: "Wed Jan 01 2014 02:34:53 GMT+0530 (IST)"
  },{  
    value: 23,
    file_date: "Thu Jan 02 2014 06:34:53 GMT+0530 (IST)"
  },{  
    value: 23,
    file_date: "Thu Jan 02 2014 09:34:53 GMT+0530 (IST)"
  },{  
    value: 23,
    file_date: "Thu Jan 02 2014 04:34:53 GMT+0530 (IST)"
  }
];

var userInput = 1; // This is the date they typed in

// This function returns all records for a specific day (integer)
var getAllDayData = function(day){
  var response = [];

  for(var i=0;i<test.length;i++){
    var fileDate = new Date(test[i].file_date);
    if(fileDate.getDate() === day){ // getDate gets you day of month
      response.push(fileDate);
    }
  }

  return response;
};

var getTotalHoursFromDates = function(data){
  var totalHours = 0;
  for(var i=0;i<data.length;i++){
    totalHours += data[i].getHours();
  }

  return totalHours;
};

var getTotalHoursFromInput = function(day){
  var dayData = getAllDayData(day);
  var response = getTotalHoursFromDates(dayData);

  return response;
};

// Call the function
var dayTotal = getTotalHoursFromInput(userInput);
console.log(dayTotal); // 58

关于获取每周小时数的请求:您必须添加一个函数,该函数需要一周中的所有天数(输入),收集每一天的所有信息,添加然后添加所有在一起的那几个小时。这是上面代码的延续。

var week = [31,1,2,3,4,5,6]; // This is the days of the selected full week

var getAllWeekData = function(days){
  var response = [];

  for(var i=0;i<days.length;i++){
    var day = getAllDayData(days[i]);
    if(day[0] != null) { // Checks for undefined or null
      for(var y=0;y<day.length;y++){
        response.push(day[i]);
      }
    }
  }

  return response;
};

var getAllWeekDays = function(selectedDays){
  var allDays = getAllWeekData(selectedDays);
  var response = getTotalHoursFromDates(allDays);

  return response;
};

var weekTotal = getAllWeekDays(week);
console.log(weekTotal); // 120

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    相关资源
    最近更新 更多