C# Program for Conversion Between 12-Hour and 24-Hour Time Format

24hour-12hour

C# programs for conversion between 12-hour time format and 24-hour format have been shown here. The 12-hour time format and the 24-hour time format are two different ways of representing time.

The 12-hour time format is a system in which the day is divided into two 12-hour periods: AM / am (ante meridiem) and PM / pm (post meridiem). In ante meridiem, the hours from 1 to 12 are used to represent the time from midnight to noon, while in post meridiem the hours from 1 to 12 are used to represent the time from noon to midnight. On the other hand, the 24-hour time format is a system in which the day is divided into 24 hours. In this system, the hours from 0 to 23 are used to represent the time from midnight to midnight.

For example, 06:30 am in the 12-hour format is equivalent to 06:30 in the 24-hour format, while 06:30 pm in the 12-hour format is equivalent to 18:30 in the 24-hour format.






1. Program & Output for Conversion Between 12-Hour Time Format and 24-Hour Format




1.1. C# Program & Output to convert 12 hour time format to 24 hour format

Code has been copied
/*****************************
       alphabetacoder.com
 C# program to convert 12 hour 
 time format to 24 hour format
******************************/

using System;
namespace TimeFormat {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int h, m, h1, m1;
            string time, am_pm;

            // take input
            Console.Write("Enter the time in 12-hour format (hh:mm am/pm): ");
            time = Console.ReadLine();

            // split input in hour, minute, am or pm
            string[] p = time.Split(":");
            // get hour
            h = Convert.ToInt32(p[0]);
            // get minute 
            m = Convert.ToInt32((p[1].Split(" "))[0]);
            // get am_pm 
            am_pm = (p[1].Split(" "))[1];

            // copy value
            h1 = h;
            m1 = m;

            // Adjust the hour
            if (am_pm.Equals("am") || am_pm.Equals("AM")) { // if it is am / AM
                // if hour = 12, then it is 0 in 24 hour format
                if (h1 == 12) {
                    h1 = 0;
                }
                // display result
                Console.WriteLine(h + ":" + m + " a.m is same as " + String.Format("{0:00}", h1) + ":" + String.Format("{0:00}", m1) + " in 24-hour format");
            } else if (am_pm.Equals("pm") || am_pm.Equals("PM")) { // if it is pm / PM
                // if hour not equals 12, then add 12 in 24 hour format    
                if (h1 != 12) {
                    h1 += 12;
                }
                // display result
                Console.WriteLine(h + ":" + m + " p.m is same as " + String.Format("{0:00}", h1) + ":" + String.Format("{0:00}", m1) + " in 24-hour format");
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the time in 12-hour format (hh:mm am/pm): 5:45 pm

05:45 p.m is same as 17:45 in 24-hour format




1.2. C# Program & Output to convert 24 hour time format to 12 hour format

Code has been copied
/*****************************
       alphabetacoder.com
 C# program to convert 24 hour 
 time format to 12 hour format
******************************/

using System;
namespace TimeFormat {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int h, m, h1, m1;
            string time;

            // take input
            Console.Write("Enter the time in 24-hour format (hh:mm): ");
            time = Console.ReadLine();

            // split input in hour, minute
            string[] p = time.Split(":");
            // get hour
            h = Convert.ToInt32(p[0]);
            // get minute 
            m = Convert.ToInt32(p[1]);

            // copy value
            h1 = h;
            m1 = m;

            // Adjust the hour
            if (h1 < 12) { // if hour value less than 12
                // display the result
                Console.WriteLine(h + ":" + m + " is same as " + String.Format("{0:00}", h1) + ":" + String.Format("{0:00}", m1) + " a.m in 12-hour format");
            } else { // if hour value greater or equal to 12
                // subtract 12 from hour value
                h1 = h1 - 12;
                Console.WriteLine(h + ":" + m + " is same as " + String.Format("{0:00}", h1) + ":" + String.Format("{0:00}", m1) + " p.m in 12-hour format");
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the time in 24-hour format (hh:mm): 16:20

16:20 is same as 04:20 p.m in 12-hour format