Date range overlap duration

node v14.17.4
version: 1.0.0
endpointsharetweet
// 00:00 - 04:00 const A = { start: new Date(2021, 6, 21, 24, 0), end: new Date(2021, 6, 22, 4, 0) } // 22:00 - 01:00 const B = { start: new Date(2021, 6, 21, 22, 0), end: new Date(2021, 6, 22, 1, 0) } console.log('A', { start: new Date(2021, 6, 21, 0, 0).toLocaleTimeString('sr-SR'), end: new Date(2021, 6, 22, 4, 0).toLocaleTimeString('sr-SR') }) console.log('B', { start: new Date(2021, 6, 21, 22, 0).toLocaleTimeString('sr-SR'), end: new Date(2021, 6, 22, 1, 0).toLocaleTimeString('sr-SR') })
const { differenceInHours, isBefore, isAfter } = require('date-fns') const resultRange = { start: isAfter(A.start, B.start) ? A.start : B.start, end: isBefore(A.end, B.end) ? A.end : B.end } console.log( differenceInHours(resultRange.end, resultRange.start) )
Loading…

no comments

    sign in to comment