# How can I get correct remaining Hours from two times in javascript? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can I get correct remaining Hours from two times in javascript? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m trying to calculate the time remaining hours based on the difference of two times passing parameter in my function. I am passing NetHrs , time2 as an argument in a javascript function but getting wrong remaining hours after calculation

1. I am passing NetHrs 7.30(intime), WorkHrs(outtime) as 3.22 Then the result should be given as 4.08. But it gives 4.80
2. If I am passing NetHrs 7.30 WorkHrs as 3.10 Then the result should be 4.20. This gives correct result But in first case my function gives wrong result. Please help Here is my function

```function GetNetHrs(inTime, outTime) {
inTime = inTime.replace('.', ':');
outTime = outTime.replace('.', ':');

var indate = new Date("01/01/2018 " + inTime + ":00");
var outdate = new Date("01/01/2018 " + outTime + ":00");
var diff = indate.getTime() - outdate.getTime();

var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);

console.log(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;
var netHrsStr = hh + "." + mm;
}

console.log(GetNetHrs("7.30","3.22"))
console.log(GetNetHrs("7.30","3.10"))```

You need to ensure your `mm` value is `0n` not `n` – you can use `padStart` for that., but you must first turn it back to a string.

```function GetNetHrs(inTime, outTime) {
inTime = inTime.replace('.', ':');
outTime = outTime.replace('.', ':');

var indate = new Date("01/01/2018 " + inTime + ":00");
var outdate = new Date("01/01/2018 " + outTime + ":00");
var diff = indate.getTime() - outdate.getTime();

var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);

console.log(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;