node.jsmomentjs

Count and check difference in timer countdown


I have timer countdown which starts from for example 04:00 (minutes:seconds). And I want to check that timer is in diapason of 10 seconds. I understand that need to convert time string to time and then check for diapason difference. But can not find a solution. To convert string time I use moment:

moment('04:00', "mm:ss")

And then I need to check that timer is in diapason in 10 second around of 4 minutes. Because input timer can be 03:58

Let's say

time1 = '04:00'

time2 = '03:57'

Then I convert both to time and need to check that time2 is around in 10 seconds from time1


Solution

  • found a solution

    const moment = require('moment')
        
    const str1 = "04:00"
    const str2 = "03:58"
    const m1 = moment(str1, "mm:ss")
    const m2 = moment(str2, "mm:ss")
    console.log(`Difference is ${Math.abs(m2.diff(m1, 'seconds'))} seconds`)