Zdobądź różnicę minut między dwoma czasami na podstawie AM, PM
// Convert minutes differnce based on am pm eg:(1:53am-1:58am > 7 minutes, 00:07 hours )(4:53pm-4:52am > 719 minutes 11:59 hours)
const formatTo24Hours = (date) => {
const [hour, minute] = date.split(':').map(parseFloat);
return date.includes('am')
? `${hour % 12}:${minute % 60}`
: `${(hour % 12) + 12}:${minute % 60}`;
};
const dateToHours = (date) => {
let [hour, minute] = date.split(':').map(parseFloat);
return `${hour}.${minute}`;
};
const getMinutes = (date) => {
let [startDate, endDate] = date
.split('-')
.map((date) => formatTo24Hours(date))
.map(dateToHours)
.map((str) =>
str
.split('.')
.map((str) => str.toString().padStart(2, 0))
.join('.')
);
let hours, minutes, remainingMinutes, difference, remain;
if (startDate > endDate) {
difference = 24.0 - startDate + Number(endDate);
} else {
difference = endDate - startDate
}
remain = (difference - 0.4 >= 0 ? difference - 0.4 : difference).toFixed(2);
remainingMinutes = Number(remain.toString().split('.')[1]);
hours = Math.trunc(remain);
minutes = hours * 60 + remainingMinutes;
hours = Math.trunc(minutes / 60);
remainingMinutes = minutes % 60;
return `Difference between time is ${minutes} total minutes and hours ${hours}:${remainingMinutes
.toString()
.padStart(2, 0)}`;
};
show(getMinutes('4:53pm-12:00am'));
Husnain Syed