C# 24 Hour Time FormatsUse special logic to handle 24-hour time parsing. Create DateTimes with the constructor.
24-hour time formats. A 24-hour clock can be converted in C# code. We can change this format to a DateTime structure for easier usage in a program.
Parsing helper methods. Sometimes parsing dates ourselves can be helpful. Parsing is challenging if there are inconsistencies or invalid dates in your data—which often occur.
An example. This code is helpful when you have only the 24-hour time figure. Sometimes the DateTime parsing methods in the base class libraries are not useful.
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 get integers.
Finally The new DateTime is returned. This technique for using 24-hour string times is useful.
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); } } 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); } } }
0738 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
A note. First, 24-hour times are based on the scale 0-23, and they are more logical because there is no difference between 11 AM and 11 PM. They are shorter to write.
DateTime.Parse. There are some problems with the DateTime parsing methods. First, they require all parts of the time to be present, and the default is an empty (zero) time.
Therefore If you have several numbers, it is easier to use the DateTime constructor.
Update in 2021. This code is not perfect, but it still has some utility. Often when dealing with data files, we must preprocess them in some way to make the data easier to handle.
Summary. We constructed DateTimes from 24-hour based time strings. It makes sense to simply parse the string and then use the DateTime constructor.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on 9/16/2022 (grammar).
© 2007-2022 sam allen.
see site info on the changelog.