We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >
• mgebhard

All-Star

54101 Points

24288 Posts

### Re: To find net hrs from the given intime and out time

#### polachan

I know there is logical error when I compare the same date and hrs are different dates. I am looking for the solution with advised code from the experts how to to find the net hrs  by avoiding the dates to calculate the difference in Hrs.

I understand that you want the community to write your code.

I explained the logical error in your code which should be enough information .  You hard coded the dates but in this edge case the hours span two days.  Your code is doing this...

`[05/25/2018 23:30:00] - [05/25/2018 01:30:00]`

When it should be doing this...

`[05/25/2018 23:30:00] - [05/26/2018 01:30:00]`

You can use an if to test if the start time (23.3) is greater than the end time (1.3).  Then do the proper calculation.  Keep in mind this is not a fool proof solution but it should work if the the hours are under 24.

I think with this information you should be able to craft a solution.  Looking forward to seeing what you come up with.

```        function CalculateBreakDownRemainingHrs() {
var fromTime = \$('#TimeStart').val().replace('.', ':');
var toTime = \$('#TimeEnd').val().replace('.', ':');
var fromdate = new Date("05/25/2018 " + fromTime + ":00");
var todate = new Date("05/25/2018 " + toTime + ":00");

if (fromdate > todate) {
todate = new Date("05/26/2018 " + toTime + ":00");
}

var msec = todate.getTime() - fromdate.getTime();
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;
netHrs = hh + "." + mm;
\$("#NetHrs").val(netHrs);
}```