Search code examples
javascriptmomentjsdayjs

Difference between two time using dayjs


I have two inputs of time and I want to get the difference/time-interval between these two using dayjs

fromtime = '13:00'
totime = '17:00'

So the output for the above two should be 4:00 hours

I tried

console.log(
          dayjs(fromtime).diff(dayjs(totime), "hours")
        );

But am not getting the expected output.


Solution

  • I found the solution to this.

    const fromtime = '11:20'
    const totime = '12:30'
    
    const ft = dayjs(`2000-01-01 ${fromtime}`);
    const tt = dayjs(`2000-01-01 ${totime}`);
    const mins = tt.diff(ft, "minutes", true);
    const totalHours = parseInt(mins / 60);
    const totalMins = dayjs().minute(mins).$m
    

    This will give the output as totalHours = 1 and totalMins = 10.
    Hope this help someone.