102. Convert Hours to Military Time
Description
Convert hours to military time (00:00 for 12:00 am, 13:00 for 1:00 pm, 23:00 for 11:00 pm, etc.).
Code
// Change hours to military time
IF AmPm-m = "am"
IF Hour-n = 12
SET Hour-n TO 0
END IF
ELSE IF AmPm-m = "pm"
IF Hour-n < 12
SET Hour-n TO Hour-n + 12
END IF
END IF
Explanation
The simplest way to perform time calculations is to first convert the hours value to military time (00:00 for 12:00 am, 13:00 for 1:00 pm, 23:00 for 11:00 pm). This calculation demonstrates how to do this.
Variables:
• Hour-n - A number variable containing the hours value.
• Min-n - A number variable containing the minutes value.
• AmPm-m - A multiple-choice variable the user can use to select am or pm.
This computation assumes that you have stored hours and minutes in separate number variables. If your time value is in a text variable, you should use Computation 99: Parsing Time Values.
To make the conversion, we first check AmPm-m to see whether Hour-n is an am or pm value. If the time is am and Hour-n is 12, we will need to change the Hour-n variable to 0. If the time is pm and Hour-n is 1-11, we will need to add 12 hours to the Hour-n variable.
With the hours converted to military time, you can now do time calculations such as Computation #0100: Elapsed Time, and Computation #0101: Total Time.