how to calculate average time with 11:59:00 PM and 12:00:00 AM so that it can be 11:59:30 PM. currently this code is giving me 11:59:30 AM
JavaScript
x
24
24
1
var convertTimeToMilliSecondsTest = function(time) {
2
var startDate = "1970/01/01";
3
if(time.indexOf("AM") != -1) {
4
newTime = time.replace("AM", " AM");
5
} else if(time.indexOf("PM") != -1) {
6
newTime = time.replace("PM", " PM");
7
}
8
var dateString = startDate + " " + newTime;
9
var date = new Date(dateString.replace(/-/g, '/'));
10
return date.getTime();
11
}
12
13
var calculateAverageTimeToBed = function(dataset) {
14
var totalTimeInMilliSeconds = 0;
15
16
for(var i = 0;i < dataset.length; ++i) {
17
totalTimeInMilliSeconds += convertTimeToMilliSecondsTest(dataset[i].startTime);
18
}
19
20
var averageTimeInBed = totalTimeInMilliSeconds / dataset.length;
21
22
return averageTimeInBed;
23
}
24
Advertisement
Answer
This is a function for if you are looking for an average time given a set of times, regardless of date, given a 24 hr period. It works for 12am – 12pm but not 12pm – 12am (because that spans 2 days). If you are spanning days, you must use my first answer which requires the entire date be given in the time being evaluated.
JavaScript
1
58
58
1
// function will determine average time given a set of
2
// times in a 24 hr. period, i.e. 12am - 12pm
3
// it does NOT work for a 24 hr. period from 12pm - 12am
4
var times = ['11:59:00 AM', '12:00:00 AM'];
5
6
// function accepts an array of times as the argument
7
// requires time to be structured as above
8
function getAverageTime(times) {
9
var count = times.length
10
var timesInSeconds = [];
11
// loop through times
12
for (var i =0; i < count; i++) {
13
// parse
14
var pieces = times[i].split(':');
15
var ampm = pieces[2].split(' ');
16
var hrs = Number(pieces[0]);
17
var mins = Number(pieces[1]);
18
var secs = Number(ampm[0]);
19
var ampm = ampm[1];
20
// convert to 24 hr format (military time)
21
if (ampm == 'PM') hrs = hrs + 12;
22
// find value in seconds of time
23
var totalSecs = hrs * 60 * 60;
24
totalSecs += mins * 60;
25
totalSecs += secs;
26
// add to array
27
timesInSeconds[i] = totalSecs;
28
}
29
// find average timesInSeconds
30
var total = 0;
31
console.log(timesInSeconds);
32
for (var j =0; j < count; j++) {
33
total = total + Number(timesInSeconds[j]);
34
}
35
var avg = Math.round(total / count);
36
console.log('avg secs: '+avg);
37
// turn seconds back into a time
38
var avgMins = Math.floor(avg/60);
39
var avgSecs = avg - (60 * avgMins);
40
var avgHrs = Math.floor(avgMins/60);
41
console.log('hours: '+avgHrs);
42
avgMins = avgMins - (60 * avgHrs);
43
// convert back to 12 hr. format
44
var avgAmpm = 'AM';
45
if (avgHrs > 12) {
46
avgAmpm = 'PM';
47
avgHrs = avgHrs - 12;
48
}
49
// add leading zeros for seconds, minutes
50
avgSecs = ('0' + avgSecs).slice(-2);
51
avgMins = ('0' + avgMins).slice(-2);
52
// your answer
53
return avgHrs+':'+avgMins+':'+avgSecs+' '+avgAmpm;
54
}
55
56
// execute
57
alert(getAverageTime(times));
58