My current challenge involves calculating the time difference using a specific 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
}
I provided calcTimeDiff('08:45', '16:00')
as an example where the expected output is 07:15
. Surprisingly, I am getting 08:15
. My suspicion leans towards timezone discrepancies causing this discrepancy.
In my attempt to debug the code, I came across the following findings:
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