Convert AM/PM time to 24 hours format?
Solution 1:
If you need to convert a string to a DateTime you could try
DateTime dt = DateTime.Parse("01:00 PM"); // No error checking
or (with error checking)
DateTime dt;
bool res = DateTime.TryParse("01:00 PM", out dt);
Variable dt
contains your datetime, so you can write it
dt.ToString("HH:mm");
Last one works for every DateTime var you have, so if you still have a DateTime, you can write it out in this way.
Solution 2:
You'll want to become familiar with Custom Date and Time Format Strings.
DateTime localTime = DateTime.Now;
// 24 hour format -- use 'H' or 'HH'
string timeString24Hour = localTime.ToString("HH:mm", CultureInfo.CurrentCulture);
Solution 3:
Convert a string to a DateTime, you could try
DateTime timeValue = Convert.ToDateTime("01:00 PM");
Console.WriteLine(timeValue.ToString("HH:mm"));
Solution 4:
int hour = your hour value;
int min = your minute value;
String ampm = your am/pm value;
hour = ampm == "AM" ? hour : (hour % 12) + 12; //convert 12-hour time to 24-hour
var dateTime = new DateTime(0,0,0, hour, min, 0);
var timeString = dateTime.ToString("HH:mm");