Problems based on date and time are common in technical interviews these days. You need to understand the basics of strings and date-time to solve these types of problems. In this article, you'll learn how to convert time from 12-hour format to 24-hour format using C++, Python, JavaScript, and C.

Problem Statement

You're given time in 12-hour format, you need to convert the time to 24-hour format (military time).

Example 1: Let time = 10:20:30 PM

Time in 24-hour format = 22:20:30

Thus the output is 22:20:30

Example 2: Let time = 06:10:00 AM

Time in 24-hour format = 06:10:00

Thus the output is 06:10:00

Example 3: Let time = 12:55:10 AM

Time in 24-hour format = 00:55:10

Thus the output is 00:55:10

Conditions for 12-Hour and 24-Hour Time Format:

  1. Midnight: Midnight is 12:00:00 AM in 12-hour time format and 00:00:00 in 24-hour time format.
  2. Noon: Noon is 12:00:00 PM in 12-hour time format and 12:00:00 in 24-hour time format.

C++ Program to Convert Time in 12-Hour Format to 24-Hour Format

Below is the C++ program to convert time in 12-hour format to 24-hour format:

        // C++ program to convert time from
// 12-hour format to 24-hour format
#include <iostream>
using namespace std;


// Function to convert time from
// 12-hour format to 24-hour format
void convertTime(string t)
{
    // Extracting hours
    int h1 = (int)t[1] - '0';
    int h2 = (int)t[0] - '0';
    int HH = (h2 * 10 + h1 % 10);

    // If the given time is in "AM"
    if (t[9] == 'A')
    {
        if (HH == 12)
        {
            cout << "00";
            for (int i=2; i <= 7; i++)
            {
                cout << t[i];
            }
        }
        else
        {
            for (int i=0; i <= 7; i++)
            {
                cout << t[i];
            }
        }
    }
    // If the given time is in "PM"
    else
    {
        if (HH == 12)
        {
            cout << "12";
            for (int i=2; i <= 7; i++)
            {
                cout << t[i];
            }
        }
        else
        {
            HH = HH + 12;
            cout << HH;
            for (int i=2; i <= 7; i++)
            {
                cout << t[i];
            }
        }
    }
    cout << endl;
}

int main()
{
    string t1 = "10:20:30 PM";
    cout << "Time in 12-hour format: " << endl;
    cout << t1 << endl;
    cout << "Time in 24-hour format: " << endl;
    convertTime(t1);

    string t2 = "06:10:00 AM";
    cout << "Time in 12-hour format: " << endl;
    cout << t2 << endl;
    cout << "Time in 24-hour format: " << endl;
    convertTime(t2);

    string t3 = "12:55:10 AM";
    cout << "Time in 12-hour format: " << endl;
    cout << t3 << endl;
    cout << "Time in 24-hour format: " << endl;
    convertTime(t3);

    return 0;
}

Output:

        Time in 12-hour format: 
10:20:30 PM
Time in 24-hour format:
22:20:30
Time in 12-hour format:
06:10:00 AM
Time in 24-hour format:
06:10:00
Time in 12-hour format:
12:55:10 AM
Time in 24-hour format:
00:55:10

Related: How Do You Find the ASCII Value Of a Character?

Python Program to Convert Time in 12-Hour Format to 24-Hour Format

Below is the Python program to convert time in 12-hour format to 24-hour format:

        # Python program to convert time from
# 12-hour format to 24-hour format


# Function to convert time from
# 12-hour format to 24-hour format
def convertTime(t):
    # Extracting hours
    h1 = ord(t[1]) - ord('0')
    h2 = ord(t[0]) - ord('0')
    HH = (h2 * 10 + h1 % 10)

    # If the given time is in "AM"
    if (t[9] == 'A'):
        if (HH == 12):
            print('00', end = '')

            for i in range(2, 8):
                print(t[i], end = '')

        else:
            for i in range(0, 8):
                print(t[i], end = '')

    # If the given time is in "PM"
    else:
        if (HH == 12):
            print("12", end = '')

            for i in range(2, 8):
                print(t[i], end = '')

        else:
            HH = HH + 12;
            print(HH, end = '')

            for i in range(2, 8):
                print(t[i], end = '')

t1 = "10:20:30 PM"
print("Time in 12-hour format: ")
print(t1)
print("Time in 24-hour format: ")
convertTime(t1)
print()

t2 = "06:10:00 AM"
print("Time in 12-hour format: ")
print(t2)
print("Time in 24-hour format: ")
convertTime(t2)
print()

t3 = "12:55:10 AM"
print("Time in 12-hour format: ")
print(t3)
print("Time in 24-hour format: ")
convertTime(t3)
print()

Output:

        Time in 12-hour format: 
10:20:30 PM
Time in 24-hour format:
22:20:30
Time in 12-hour format:
06:10:00 AM
Time in 24-hour format:
06:10:00
Time in 12-hour format:
12:55:10 AM
Time in 24-hour format:
00:55:10

Related: How to Find the Sum of Natural Numbers Using Recursion

JavaScript Program to Convert Time in 12-Hour Format to 24-Hour Format

Below is the JavaScript program to convert time in 12-hour format to 24-hour format:

        // JavaScript program to convert time from
// 12-hour format to 24-hour format


// Function to convert time from
// 12-hour format to 24-hour format
function convertTime(t) {
    // Extracting hours
    var h1 = Number(t[1] - '0');
    var h2 = Number(t[0] - '0');
    var HH = (h2 * 10 + h1 % 10);

    // If the given time is in "AM"
    if (t[9] == 'A')
    {
        if (HH == 12)
        {
            document.write("00");
            for (let i=2; i <= 7; i++)
            {
                document.write(t[i]);
            }
        }
        else
        {
            for (let i=0; i <= 7; i++)
            {
                document.write(t[i]);
            }
        }
    }
    // If the given time is in "PM"
    else
    {
        if (HH == 12)
        {
            document.write("12");
            for (let i=2; i <= 7; i++)
            {
                document.write(t[i]);
            }
        }
        else
        {
            HH = HH + 12;
            document.write(HH);
            for (let i=2; i <= 7; i++)
            {
                document.write(t[i]);
            }
        }
    }
    document.write("
");
}


var t1 = "10:20:30 PM";
document.write("Time in 12-hour format: " + "
");
document.write(t1 + "
");
document.write("Time in 24-hour format: " + "
");
convertTime(t1);

var t2 = "06:10:00 AM";
document.write("Time in 12-hour format: " + "
");
document.write(t2 + "
");
document.write("Time in 24-hour format: " + "
");
convertTime(t2);

var t3 = "12:55:10 AM";
document.write("Time in 12-hour format: " + "
");
document.write(t3 + "
");
document.write("Time in 24-hour format: " + "
");
convertTime(t3);

Output:

        Time in 12-hour format: 
10:20:30 PM
Time in 24-hour format:
22:20:30
Time in 12-hour format:
06:10:00 AM
Time in 24-hour format:
06:10:00
Time in 12-hour format:
12:55:10 AM
Time in 24-hour format:
00:55:10

Related: How to Count the Occurrences of a Given Character in a String

C Program to Convert Time in 12-Hour Format to 24-Hour Format

Below is the C program to convert time in 12-hour format to 24-hour format:

        // C program to convert time from
// 12-hour format to 24-hour format
#include <stdio.h>
#include <string.h>


// Function to convert time from
// 12-hour format to 24-hour format
void convertTime(char t[])
{
    // Extracting hours
    int h1 = t[1] - '0';
    int h2 = t[0] - '0';
    int HH = (h2 * 10 + h1 % 10);

    // If the given time is in "AM"
    if (t[9] == 'A')
    {
        if (HH == 12)
        {
            printf("00");
            for (int i=2; i <= 7; i++)
            {
                printf("%c", t[i]);
            }
        }
        else
        {
            for (int i=0; i <= 7; i++)
            {
                printf("%c", t[i]);
            }
        }
    }
    // If the given time is in "PM"
    else
    {
        if (HH == 12)
        {
            printf("12");
            for (int i=2; i <= 7; i++)
            {
                printf("%c", t[i]);
            }
        }
        else
        {
            HH = HH + 12;
            printf("%d", HH);
            for (int i=2; i <= 7; i++)
            {
                printf("%c", t[i]);
            }
        }
    }
    printf("\⁠n");
}

int main()
{
    char t1[] = "10:20:30 PM";
    printf("Time in 12-hour format: \⁠n");
    printf("%s \⁠n", t1);
    printf("Time in 24-hour format: \⁠n");
    convertTime(t1);

    char t2[] = "06:10:00 AM";
    printf("Time in 12-hour format: \⁠n");
    printf("%s \⁠n", t2);
    printf("Time in 24-hour format: \⁠n");
    convertTime(t2);

    char t3[] = "12:55:10 AM";
    printf("Time in 12-hour format: \⁠n");
    printf("%s \⁠n", t3);
    printf("Time in 24-hour format: \⁠n");
    convertTime(t3);

    return 0;
}

Output:

        Time in 12-hour format: 
10:20:30 PM
Time in 24-hour format:
22:20:30
Time in 12-hour format:
06:10:00 AM
Time in 24-hour format:
06:10:00
Time in 12-hour format:
12:55:10 AM
Time in 24-hour format:
00:55:10

Related: An Introduction to the Merge Sort Algorithm

Learn More About Common Algorithms

Algorithms are the core of programming. They're integral in helping to solve any and all programming problems. You must know about the most common algorithms like Dijkstra’s Algorithm, Merge Sort, Quicksort, Depth First Search, Breadth-First Search, Binary Search, etc. if you want to be the most efficient programmer you can be.