C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: You may be working on a program that receives 24-hour times from an interface, but you want to process them in DateTime format.
WriteMilitaryTime: This method divides the 24-hour time string and turns it into 2 substrings. Then we use int.Parse to turn the two parts to integers.
Finally: The new DateTime is returned. You will need to modify some aspects of the method, but the technique for using 24-hour string times is useful.
Class that helps with military times: C#
using System;
public static class MilitaryTime
{
    public static void WriteMilitaryTime(DateTime date)
    {
        //
        // Convert hours and minutes to 24-hour scale.
        //
        string value = date.ToString("HHmm");
        Console.WriteLine(value);
    }
    public static DateTime ParseMilitaryTime(string time,
            int year, int month, int day)
    {
        //
        // Convert hour part of string to integer.
        //
        string hour = time.Substring(0, 2);
        int hourInt = int.Parse(hour);
        if (hourInt >= 24)
        {
            throw new ArgumentOutOfRangeException("Invalid hour");
        }
        //
        // Convert minute part of string to integer.
        //
        string minute = time.Substring(2, 2);
        int minuteInt = int.Parse(minute);
        if (minuteInt >= 60)
        {
            throw new ArgumentOutOfRangeException("Invalid minute");
        }
        //
        // Return the DateTime.
        //
        return new DateTime(year, month, day, hourInt, minuteInt, 0);
    }
}
Then: It uses the ParseMilitaryTime method to transform the time strings with hours and minutes in 24-hour format into valid DateTimes.
Foreach: The foreach-loop uses the time figures for April 27, 2009. This works because those figures indicate a valid DateTime.
ForeachWarning: If you try to make a DateTime with an invalid figure, the constructor will fail.
C# program that turns military times into DateTimes
using System;
class Program
{
    static void Main()
    {
        //
        // Copy the current time into DateTime struct.
        //
        DateTime now = DateTime.Now;
        //
        // Write time as military time.
        //
        MilitaryTime.WriteMilitaryTime(now);
        //
        // Example military times.
        //
        string[] militaryTimes = new string[]
        {
            "0000",
            "0400",
            "0550",
            "1330",
            "1400",
            "2100",
            "2200",
            "2350",
        };
        //
        // Parse and display the times as DateTimes.
        //
        foreach (string militaryTime in militaryTimes)
        {
            DateTime time = MilitaryTime.ParseMilitaryTime(militaryTime, 2009, 4, 27);
            Console.WriteLine(time);
        }
    }
}
Output
1142
4/27/2009 12:00:00 AM
4/27/2009 4:00:00 AM
4/27/2009 5:50:00 AM
4/27/2009 1:30:00 PM
4/27/2009 2:00:00 PM
4/27/2009 9:00:00 PM
4/27/2009 10:00:00 PM
4/27/2009 11:50:00 PM
And: The United States still uses the 12-hour clock in user interfaces and programs.
24-hour clock: WikipediaTherefore: If you have several numbers, it is easier to use the DateTime constructor.
DateTime.Parse