Khuram Malik
Khuram Malik

Reputation: 1485

Working out the numbers of hours elapsed between two dates using datejs and timejs

I'm using the datejs library with timejs module. We receive contact requests into a table and want to work out our response times. So i have start date with timestamp and response date.

I used date.js to work out the time difference between the two dates. Problem is that it does a simple math calculation between the two numbers provided from each timestamp rather than taking into account that 24 hours elapse for each day difference.

So for example:

var start = Date.parse("Feb 16 2012 15:30:00"); 
var end = Date.parse("Feb 19 2012 09:30:00");

var span = new TimeSpan(end - start);

var tdiff = span.hours; 


console.log(tdiff);

This gives me a result of 18 hours, when the actual time elapsed between those two dates is 66hours. How can i use this library to give me the actual time elapsed?

Secondly, the working day is from 9am to 5.30pm, how can i exclude the hours that are not part of the working day from this calculation?

Upvotes: 1

Views: 691

Answers (1)

Sunil Kumar B M
Sunil Kumar B M

Reputation: 2795

var start = Date.parse("Feb 16 2012 15:30:00"); 
var end = Date.parse("Feb 19 2012 09:30:00");

var numOfHrs = (end-start) / (1000*60*60);
alert(numOfHrs);
alert((24-15.5) * (numOfHrs/24));

Upvotes: 1

Related Questions