如何计算 11:59:00 PM 和 12:00:00 AM 的平均时间,以便它可以是 11:59:30 PM。目前这段代码给了我 11:59:30 AM
var convertTimeToMilliSecondsTest = function(time) {
var startDate = "1970/01/01";
if(time.indexOf("AM") != -1) {
newTime = time.replace("AM", " AM");
} else if(time.indexOf("PM") != -1) {
newTime = time.replace("PM", " PM");
}
var dateString = startDate + " " + newTime;
var date = new Date(dateString.replace(/-/g, '/'));
return date.getTime();
}
var calculateAverageTimeToBed = function(dataset) {
var totalTimeInMilliSeconds = 0;
for(var i = 0;i < dataset.length; ++i) {
totalTimeInMilliSeconds += convertTimeToMilliSecondsTest(dataset[i].startTime);
}
var averageTimeInBed = totalTimeInMilliSeconds / dataset.length;
return averageTimeInBed;
}