Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to convert time from 12 hour to 24 hour format
Converting time from 12-hour format to 24-hour format in C# is a common requirement in applications. The DateTime.Parse() method can parse 12-hour time strings, and the ToString() method with the "HH:mm" format specifier converts it to 24-hour format.
Syntax
Following is the syntax for parsing 12-hour time and converting to 24-hour format −
DateTime dateTime = DateTime.Parse("12-hour time string");
string time24Hour = dateTime.ToString("HH:mm");
The HH format specifier represents hours in 24-hour format (00-23), while mm represents minutes (00-59).
Using DateTime.Parse() for Basic Conversion
The simplest approach uses DateTime.Parse() to convert a 12-hour time string to 24-hour format −
using System;
public class Program {
public static void Main(string[] args) {
DateTime d = DateTime.Parse("05:00 PM");
Console.WriteLine("12-hour format: 05:00 PM");
Console.WriteLine("24-hour format: " + d.ToString("HH:mm"));
DateTime morning = DateTime.Parse("08:30 AM");
Console.WriteLine("12-hour format: 08:30 AM");
Console.WriteLine("24-hour format: " + morning.ToString("HH:mm"));
}
}
The output of the above code is −
12-hour format: 05:00 PM 24-hour format: 17:00 12-hour format: 08:30 AM 24-hour format: 08:30
Using DateTime.ParseExact() for Specific Formats
For more control over the input format, use DateTime.ParseExact() −
using System;
using System.Globalization;
public class Program {
public static void Main(string[] args) {
string[] times12Hour = {"11:30 PM", "06:45 AM", "12:00 PM", "12:00 AM"};
foreach(string time in times12Hour) {
DateTime dt = DateTime.ParseExact(time, "hh:mm tt", CultureInfo.InvariantCulture);
Console.WriteLine($"{time} ? {dt.ToString("HH:mm")}");
}
}
}
The output of the above code is −
11:30 PM ? 23:30 06:45 AM ? 06:45 12:00 PM ? 12:00 12:00 AM ? 00:00
Converting Multiple Time Formats
Here's a method that handles various 12-hour time formats and converts them to 24-hour format −
using System;
public class Program {
public static string ConvertTo24Hour(string time12Hour) {
try {
DateTime dt = DateTime.Parse(time12Hour);
return dt.ToString("HH:mm");
}
catch (FormatException) {
return "Invalid time format";
}
}
public static void Main(string[] args) {
string[] testTimes = {
"2:15 PM", "10:45 AM", "12:30 PM",
"12:30 AM", "9:00 PM", "1:05 AM"
};
Console.WriteLine("12-Hour ? 24-Hour Conversion");
Console.WriteLine("?????????????????????????????");
foreach(string time in testTimes) {
Console.WriteLine($"{time,8} ? {ConvertTo24Hour(time)}");
}
}
}
The output of the above code is −
12-Hour ? 24-Hour Conversion ????????????????????????????? 2:15 PM ? 14:15 10:45 AM ? 10:45 12:30 PM ? 12:30 12:30 AM ? 00:30 9:00 PM ? 21:00 1:05 AM ? 01:05
Key Points
-
DateTime.Parse()automatically recognizes AM/PM indicators and converts accordingly. -
The
"HH:mm"format specifier produces 24-hour time format. -
12:00 PM converts to 12:00 (noon) and 12:00 AM converts to 00:00 (midnight).
-
Always handle
FormatExceptionwhen parsing user input times.
Conclusion
Converting 12-hour to 24-hour format in C# is straightforward using DateTime.Parse() and the "HH:mm" format specifier. This approach handles AM/PM indicators automatically and provides accurate time conversion for various input formats.
