Denoteone
Denoteone

Reputation: 4055

Get total time and create an average based on timestamps

Background: I want to use coldfusion to find the total time a process takes by taking two timestamps and then adding all of the total times to create an average.

Question: What is the best way to take two timestamps and find out the difference in time by minutes.

Example:

Time Stamp #1:  2015-05-08 15:44:00.000
Time Stamp #2:  2015-05-11 08:52:00.000

So the time between the above timestamps would be: 2 Days 6 hours 52 mins = 3,292 minutes

I want to run this conversion on a handful of timestamp's and take the total minutes and divide to get an average.

To add more information to my question. 1. Yes the values are coming from a DB MSSQL. 2. I am actually going to be using the individual time differences and showing and overall average. So in my for loop each line will have a value like 3,292 (converted to mins or hours or days) and at the end of the for loop I want to show an average of all the lines shown on the page. Let me know if I need to add any other information.

Upvotes: 1

Views: 100

Answers (2)

Dan Bracuk
Dan Bracuk

Reputation: 20804

Assuming your query is sorted properly, something like this should work.

totalMinutes = 0;
for (i = 2; i <= yourQuery.recordcount; i++)
totalMinutes += 
    DateDiff('n'
    , yourQuery.timestampField[i-1]
    ,yourQuery.timestampField[i]);

avgMinutes = totalMinutes / (yourQuery.recordcount -1);  

Upvotes: 1

Matt Busche
Matt Busche

Reputation: 14333

Use the dateDiff() function

diffInMinutes = dateDiff('n', date1, date2);

Upvotes: 1

Related Questions