Python Program to Convert Time from 12 hour to 24 hour Format Last Updated : 30 Oct, 2025 Comments Improve Suggest changes 16 Likes Like Report Given a time in 12-hour AM/PM format, the task is to convert it to military (24-hour) time. For Example:Input: 11:21:30 PMOutput: 23:21:30Let’s explore different methods to achieve this conversion.Using DateTimeThis method uses Python's datetime module, which automatically handles the conversion and validates the time format. It is the most reliable and concise approach. Python from datetime import datetime t1 = datetime.strptime('11:21:30 PM', '%I:%M:%S %p') print(t1.strftime('%H:%M:%S')) t2 = datetime.strptime('12:12:20 AM', '%I:%M:%S %p') print(t2.strftime('%H:%M:%S')) Output23:21:30 00:12:20 Explanation:datetime.strptime(time, '%I:%M:%S %p') parses the 12-hour format string (%I) and AM/PM (%p) into a datetime object.strftime('%H:%M:%S') converts it into a 24-hour formatted string.Using Slicing and Conditional LogicThis method directly manipulates the string. It’s simple for well-formatted inputs but requires careful handling of edge cases (12 AM/PM). Python t = "08:05:45 PM" if t[-2:] == "AM" and t[:2] == "12": c = "00" + t[2:-2] elif t[-2:] == "AM": c = t[:-2] elif t[-2:] == "PM" and t[:2] == "12": c = t[:-2] else: c = str(int(t[:2]) + 12) + t[2:8] print(c) Output20:05:45 Explanation:Checks the AM/PM suffix (t[-2:]) and the hour part (t[:2]).Adds 12 for PM times except when hour is 12.Removes AM/PM from the final string.Using Regular ExpressionsThis method uses regex to extract hour, minute, second, and AM/PM parts, then applies arithmetic logic to convert to 24-hour format. Python import re t = '11:21:30 PM' hour, mins, sec, am_pm = re.findall(r'\d+|\w+', t) hour = int(hour) if am_pm == 'PM' and hour != 12: hour += 12 elif am_pm == 'AM' and hour == 12: hour = 0 c = f'{hour:02d}:{mins}:{sec}' print(c) t2 = '12:12:20 AM' hour, mins, sec, am_pm = re.findall(r'\d+|\w+', t2) hour = int(hour) if am_pm == 'PM' and hour != 12: hour += 12 elif am_pm == 'AM' and hour == 12: hour = 0 c2 = f'{hour:02d}:{mins}:{sec}' print(c2) Output23:21:30 00:12:20 Explanation:re.findall('\d+|\w+', t) extracts hours, minutes, seconds, and AM/PM.Conditional logic converts the hour to 24-hour format.Formatted using f-string with zero-padding ({hour:02d}). Create Quiz Comment S SaumyaBansal Follow 16 Improve S SaumyaBansal Follow 16 Improve Article Tags : Technical Scripter Python Python Programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like