Clock Arrows Angle

by AntowaKartowa

JavaScript

function getClockArrowsAngle(hours, minutes) {
	if ((hours !== undefined && typeof hours !== 'number') || (minutes !== undefined && typeof minutes !== 'number')) return false;

	hours &&= Math.abs(hours) % 12 || 12;
	minutes &&= Math.min(Math.abs(minutes), 59);
	hours ||= new Date().getHours() % 12 || 12;
	minutes ??= new Date().getMinutes();
	
	const degPerHour = 30; // 360 / 12;
	const degPerMinute = 6 // 360 / 60;
	const degHourPerMin = 0.5 // degPerHour / 60;
	
	const hAngle = hours * degPerHour + minutes * degHourPerMin;
	const mAngle = minutes * degPerMinute;
	
	const clockwise = mAngle > hAngle ? mAngle - hAngle : 360 - (hAngle - mAngle);
	const counterClockwise = 360 - clockwise;
	
	return { clockwise, counterClockwise, hours, minutes };
}

console.log(getClockArrowsAngle());
console.log(getClockArrowsAngle(12, 0));
console.log(getClockArrowsAngle(15, 15));