Home > OS >  Time difference in hh:mm format
Time difference in hh:mm format

Time:08-17

I am calculating the difference between two times with the following function:

const calcTimeDiff = (time1: string, time2: string) => {
    const timeStart = new Date()
    const timeEnd = new Date()
    const valueStart = time1.split(':')
    const valueEnd = time2.split(':')

    timeStart.setHours( valueStart[0],  valueStart[1], 0, 0)
    timeEnd.setHours( valueEnd[0],  valueEnd[1], 0, 0)

    const difference = timeEnd.getTime() - timeStart.getTime()
    return format(difference, 'HH:mm') // date-fns
}

For example calcTimeDiff('08:45', '16:00') which should yield 07:15. However, I get 08:15 instead. My guess is that it is caused by timezone conflicts.

Debugging my code gave me the following insights:

console.log(difference, timeStart, timeEnd)
Thu Jan 01 1970 08:15:00 GMT 0100, Wed Aug 17 2022 08:45:00 GMT 0200, Wed Aug 17 2022 16:00:00 GMT 0200

CodePudding user response:

If you get string in hh:mm format, you don't need Date, just calculate:

const calcTimeDiff = (time1: string, time2: string) => {
  const [h1, m1] = time1.split(':');
  const [h2, m2] = time2.split(':');
  let diff = (h2 - h1) * 60   (m2 - m1);
  if (diff < 0) diff  = 24 * 60;
  const hours = Math.floor(diff / 60);
  const minutes = diff - hours * 60;
  const hh = hours.toString().padStart(2, '0');
  const mm = minutes.toString().padStart(2, '0');
  return `${hh}:${mm}`;
}

CodePudding user response:

Why not make use of the intervalToDuration method which is included in the date-fns library. This will return an object for you like below

{years: 0, months: 0, days: 0, hours...}

It can be implemented easily into your function like so:

const calcTimeDiff = (time1: string, time2: string) => {
    const timeStart = new Date()
    const timeEnd = new Date()
    const valueStart = time1.split(':')
    const valueEnd = time2.split(':')

    timeStart.setHours( valueStart[0],  valueStart[1], 0, 0)
    timeEnd.setHours( valueEnd[0],  valueEnd[1], 0, 0)

    return intervalToDuration({ start: timeStart, end: timeEnd })
}

CodePudding user response:

Solved it this way:

const calcTimeDiff = (time1: string, time2: string) => {
    const timeStart = new Date()
    const timeEnd = new Date()
    const valueStart = time1.split(':')
    const valueEnd = time2.split(':')

    timeStart.setHours( valueStart[0],  valueStart[1], 0, 0)
    timeEnd.setHours( valueEnd[0],  valueEnd[1], 0, 0)

    const difference = new Date(timeEnd.getTime() - timeStart.getTime())

    return format(
        addMinutes(difference, difference.getTimezoneOffset()),
        'HH:mm'
    )
}

CodePudding user response:

You can use moment.js to perform what you want.

This can be done using the diff method.

const calcTimeDiff = (time1, time2) => {
    
    const start = moment(time1, 'HH:mm');   
    const end = moment(time2, 'HH:mm');   
    
    let diff = end.diff(start);
    return moment.utc(diff).format("HH:mm");
}

console.log(calcTimeDiff("8:15", "16:15"))
console.log(calcTimeDiff("7:45", "19:21"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

  • Related