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

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

**kocei**#2

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

**kocei**#4

`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");
```

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.

**kocei**#7

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?

**kocei**#9

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

**kocei**#12

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

Something like that I suppose

**flycoders_sourav**#13

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.

**kocei**#14

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";
```