Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Time Conversion
# Challenge: Time Conversion - HakerRank
# Author: Stella Miranda
# Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
# Note:
# Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock.
# Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
# Input Format:
# A single string containing a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM),
# where 1<= hh <= 12 and 0<= mm,ss<=59 .
# Output Format:
# Convert and print the given time in 24-hour format, where 00<= hh<= 23 .
# Sample Input
# 07:05:45PM
# Sample Output
# 19:05:45
time = gets.strip
temp = time.split(":")
result = ""
if temp[0].to_i<12 and temp[2].to_s[2,3] == "PM"
result = (temp[0].to_i+12).to_s+":"+temp[1]+":"+temp[2].to_s[0,2]
elsif temp[0].to_i ==12 and temp[2].to_s[2,3] == "AM"
result = "00:"+temp[1]+":"+temp[2].to_s[0,2]
result = temp[0]+":"+temp[1]+":"+temp[2].to_s[0,2]
puts result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.