Converting a normal (12-hour) time like "8:30 am" or "8:30 pm" to 24-hour time (like "0830" or "2030") sounds easy enough, right?  Well, let's see if you can do it!

You will have to define a function named "to24hourtime", and you will be given an hour (always in the range of 1 to 12, inclusive), a minute (always in the range of 0 to 59, inclusive), and a period (either "am" or "pm") as input.

Your task is to return a four-digit string that encodes that time in 24-hour time.

Difficulty: EASY
Starter code: def to24hourtime(hour, minute, period):
	

Solutions:
def to24hourtime(hour, minute, period):
	return '%02d%02d' % (hour % 12 + 12 * (period == 'pm'), minute)


from datetime import datetime

def to24hourtime(h, m, p):
	return datetime.strptime('%02d%02d%s' % (h, m, p.upper()), '%I%M%p').strftime('%H%M')


from datetime import datetime

def to24hourtime(h, m, p):
	t = f'{h}:{m} {p}'
	return datetime.strptime(t, '%I:%M %p').strftime('%H%M')


def to24hourtime(hour, minute, period):
	return f"{period.startswith('p') * 12 + hour % 12:02d}{minute:02d}"


def to24hourtime(hour, minute, period):
	if hour < 10:
		hour = '0' + str(hour)
	else:
		hour = str(hour)
	if minute < 10:
		minute = '0' + str(minute)
	else:
		minute = str(minute)
	time = hour + minute
	if period == 'pm' and int(hour) != 12:
		time = str(int(time) + 1200)
	if period == 'am' and int(hour) == 12:
		time = str(int(time) - 1200)
		if int(minute) < 10:
			time = '000' + time
		else:
			time = '00' + time
	print((hour, minute, period))
	return time


def to24hourtime(hour, minute, period):
	hour_str = str(hour)
	minute_str = str(minute)
	if period == 'am':
		if hour <= 9:
			hour_str = '0' + hour_str
		if minute <= 9:
			minute_str = '0' + minute_str
		if hour == 12:
			hour_str = '00'
		return '%s%s' % (hour_str, minute_str)
	else:
		if hour <= 12:
			hour += 12
		if hour == 24:
			hour = 0
		hour_str = str(hour)
		if hour <= 9:
			hour_str = '0' + hour_str
		if minute <= 9:
			minute_str = '0' + minute_str
		if hour == 0:
			hour_str = '12'
		return '%s%s' % (hour_str, minute_str)


def to24hourtime(hour, minute, period):
	if 'pm' in period:
		time = str(int(hour + 12)) + str(minute).zfill(2)
		if hour == 12:
			return '12' + str(minute).zfill(2)
		return time
	else:
		time = str(str(hour).zfill(2)) + str(minute).zfill(2)
		if hour == 12:
			return '00' + str(minute).zfill(2)
		return time


from datetime import datetime

def to24hourtime(hour, minute, period):
	return datetime.strptime(f'{hour} {minute} {period}', '%I %M %p').strftime('%H%M')


def to24hourtime(hour, minute, period):
	if period == 'am' and hour == 12:
		hour = '00'
	elif period == 'pm' and hour == 12:
		hour = '12'
	elif period == 'pm':
		hour += 12
	elif hour < 10:
		hour = '0' + str(hour)
	if minute < 10:
		minute = '0' + str(minute)
	return str(hour) + str(minute)


def to24hourtime(hour, minute, period):
	if hour == 12 and period == 'am':
		hour = 0
	else:
		hour += 12 if period == 'pm' and hour != 12 else 0
	return f"{str(hour).rjust(2, '0')}{str(minute).rjust(2, '0')}"