How can I calculate the difference between two times that are in 24 hour format?

assume that the two different time is like that **start time 10:27** and **end time 12.40**

now i want to calculate the hours and min from two different time

please help me anyone. how can i do that

Thanks in advance

So you have 2 differents formats or it’s a mistake ? You could probably use momentjs to solve it.

two different time is like that start time 10:27 and end time 12.40

it’s a mistake. ok let me try

`return moment('1027', 'HHmm').diff(moment('1242', 'HHmm'));`

Would do something like that, I didn’t try it so you maybe need to correct it.

EDIT :

```
let diff = moment('12:42', 'HH:mm').diff(moment('10:27', 'HH:mm'))
let d = moment.duration(diff);
return Math.floor(d.asHours()) + moment.utc(diff).format(":mm");
```

I think my solution is not the best, maybe you should try other things ahah

below my time deference screenshots

below the result

based on my time two difference time result should be shown **1:05**

but get **1:00**

below my code

```
let diff = moment(parseInt(this.endTime), 'H:m').diff(moment(parseInt(this.startTime), 'H:m'))
let d = moment.duration(diff);
let totaltime =Math.floor(d.asHours()) + moment.utc(diff).format(":mm");
alert(totaltime)
```

how can i fix that.

Can you console.log(parseInt(this.endTime)) and console.log(parseInt(this.startTime)) please ? Just take my code upper in the topic, it is working, ive tried it

its showing **NaN**

```
private startTime:number;
private endTime:number;
let diff = moment(this.endTime, 'H:m').diff(moment(this.startTime, 'H:m'))
let d = moment.duration(diff);
let totaltime =Math.floor(d.asHours()) + moment.utc(diff).format(":mm");
alert(totaltime)
```

where i’m wrong?

Why are you passing number when it’s waiting for string ? Show your variables startTime and endTime

its working properly right now

can i change the formal 1:01 to 1 hour 01 minute?

`Math.floor(d.asHours()) +" hours " + moment.utc(diff).format("mm") + " minutes";`

Something like that I suppose

now actually i want to calculate amount based on time

supercharges $40;

```
hourlyCharge : number =40;
this.totalammout= this.hourlyCharge * parseInt(this.total);
console.log("Total ammount"+this.totalammout)
let diff = moment(this.endTime, 'H:m').diff(moment(this.startTime, 'H:m'))
let d = moment.duration(diff);
this.total =Math.floor(d.asHours()) +" hours " + moment.utc(diff).format("mm") + " minutes";
//alert(this.total)
```

I try to do look like this for calculating the amount based on time

How can i do that.

I feel like Im doing your job because you are too lazy to do it yourself. Based on the fact that you are a member for at least one year on this forum, you are more than capable to achieve this alone, so it will be the last time I help you:

```
let diff = moment(this.endTime, 'HH:mm').diff(moment(this.startTime, 'HH:mm'))
let d = moment.duration(diff);
let hours = Math.floor(d.asHours());
let minutes = moment.utc(diff).format("mm");
this.totalAmount = hours * hourlyCharge + parseInt(minutes) * hourlyCharge/60;
this.total =hours +" hours " + minutes + " minutes";
```

Thank you so much…

best comment of the month