Matarishvan
Matarishvan

Reputation: 2422

momentjs find difference and display in specific format

I have from datetime displaying

2019-12-19 06:00

I have to datetime displaying

2019-12-19 09:22

I am using moment.js. I need to find the difference of these two time and should have to display in this format

3H, 55M

This is what i have tried

moment(date1).diff(moment(date2), 'hours') "," moment(date1).diff(moment(date2), 'minutes')

and i am not getting the proper format with difference.

Upvotes: 0

Views: 28

Answers (2)

Waseem Ansar
Waseem Ansar

Reputation: 123

Try this out just getting difference between two dates in minutes and then converting to hours and minutes.

var date1= moment('2019-12-19 06:00')
var date2 = moment('2019-12-19 09:22')

var diffInMin=moment.duration(date2.diff(date1)).asMinutes()
var hours = Math.floor(diffInMin / 60) + 'H'
var minutes = diffInMin % 60 + 'M'
console.log(hours +', '+ minutes)

Upvotes: 1

Rohan Veer
Rohan Veer

Reputation: 1490

I think it should be something like this:

moment(moment(toDate).diff(moment(fromDate))).format('HH[H] mm[M]')

Upvotes: 0

Related Questions