I have a list in which multiple Time-In or Time-Out , Break In or Break out. i want to calculate total duration from Time-In to Time-Out
Here is data.
const data =
[ { timeSheetDetailActivityId : 0
, timeSheetDailyActivityId : 0
, entryDateTime : '02:28'
, activityTypeId : 'Time In'
, comments : 'dgdfgdfg'
, isLeave : ''
}
, { timeSheetDetailActivityId : 0
, timeSheetDailyActivityId : 0
, entryDateTime : '03:28'
, activityTypeId : 'Time Out'
, comments : '2323'
, isLeave : ''
}
, { timeSheetDetailActivityId : 0
, timeSheetDailyActivityId : 0
, entryDateTime : '04:28'
, activityTypeId : 'Break In'
, comments : '2323'
, isLeave : ''
}
, { timeSheetDetailActivityId : 0
, timeSheetDailyActivityId : 0
, entryDateTime : '05:28'
, activityTypeId : 'Break Out'
, comments : '2323'
, isLeave : ''
}
, { timeSheetDetailActivityId : 0
, timeSheetDailyActivityId : 0
, entryDateTime : '06:28'
, activityTypeId : 'Time In'
, comments : '2323'
, isLeave : ''
}
, { timeSheetDetailActivityId : 0
, timeSheetDailyActivityId : 0
, entryDateTime : '07:28'
, activityTypeId : 'Time Out'
, comments : '232323'
, isLeave : ''
}
]
here is Input form [![you can check input fields for better understanding][1]][1]
Note : so first Time-in was 2:28 or Time-out 3:28 and hour will be 1. after break-in or break out again Time-in is 6:28 or Time-out 7:28 and hour will be 1. now hours is 2. Please any body suggest how I can get total duration 2 in using JavaScript , Typescript.
Here is minimal reproducable example to determine the difference between two time strings (formatted as 'hh:mm').
const tIN = '03:28'.split(`:`).map(Number);
const tOUT = '04:28'.split(`:`).map(Number);
let [now, then] = [new Date, new Date];
now.setHours(tIN[0]);
now.setMinutes(tIN[1]);
then.setHours(tOUT[0]);
then.setMinutes(tOUT[1]);
const diff = dateDiffCalc(then - now);
console.log(diff);
function dateDiffCalc(milliseconds) {
const secs = Math.floor(Math.abs(milliseconds) / 1000);
const mins = Math.floor(secs / 60);
const hours = Math.floor(mins / 60);
return {
days: Math.floor(hours / 24),
hours: hours % 24,
minutes: mins % 60,
seconds: secs % 60,
};
}