Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

get difference of minutes between two time based on 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'));
Comment

PREVIOUS NEXT
Code Example
Javascript :: scrape html table javascript 
Javascript :: play audio file in phaser 
Javascript :: moment min 
Javascript :: double function call javascript 
Javascript :: cai nodejs ubuntu 
Javascript :: regex 1-31 days 
Javascript :: react testing library increase debug length 
Javascript :: get the key of largest json array value 
Javascript :: setTimeout() Method in javascript 
Javascript :: How to get maximum value in Javascript 
Javascript :: chart js react 
Javascript :: redux actions.js 
Javascript :: javascript injection in mongodb 
Javascript :: Ways to Declare Variables in Vanilla JavaScript 
Javascript :: player.filter 
Javascript :: es6 get first and last element of array 
Javascript :: how to get max value from array of objects in javascript 
Javascript :: javascript set elements width by tag name 
Javascript :: inertia-link vuetify 
Javascript :: javascript non primitive data types 
Javascript :: duplicate images in webpage js 
Javascript :: node js hello word 
Javascript :: javascript object array sum of values in object 
Javascript :: create array in javascript contains 10 elements 
Javascript :: react catch error json message 
Javascript :: remove array value by index js 
Javascript :: regular expression url 
Javascript :: javascript find area of triangle 
Javascript :: react-phone-number-input properties 
Javascript :: node start is too slow windows 10 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =