Calculate the angle between hour hand and minute hand | Utility function to find minimum of two integers ; Function to calculate the angle ; validate the input ; Calculate the angles moved by hour and minute hands with reference to 12 : 00 ; Find the difference between two angles ; Return the smaller angle of two possible angles ; Driver Code
function min ( x , y ) { return ( x < y ) ? x : y ; } function calcAngle ( h , m ) { if ( h < 0 m < 0 h > 12 m > 60 ) document . write ( " " ) ; if ( h == 12 ) h = 0 ; if ( m == 60 ) { m = 0 ; h += 1 ; if ( h > 12 ) h = h - 12 ; } let hour_angle = 0.5 * ( h * 60 + m ) ; let minute_angle = 6 * m ; let angle = Math . abs ( hour_angle - minute_angle ) ; angle = min ( 360 - angle , angle ) ; return angle ; } document . write ( calcAngle ( 9 , 60 ) + " " ) ; document . write ( calcAngle ( 3 , 30 ) + " " ) ;