I need to convert 12 hours format time (am/pm) to 24 hours format time, e.g. 01:00 PM to 13:00 using C#. How can I convert it?
-
what is the format of the source data? – phoog Dec 01 '11 at 07:02
-
What is the type of the format? Do you want toi cas a string or a DateTime? – JiBéDoublevé Dec 01 '11 at 07:02
-
@phoog format is h:mm tt – nmathur Dec 01 '11 at 07:03
-
`DateTime` doesn't have such thing like format. Its applied only when you're using `ToString`, so are your variables of type `string` ? – Piotr Auguscik Dec 01 '11 at 07:03
-
@nmathur is it a string or a DateTime value? – phoog Dec 01 '11 at 07:04
-
`HH` should get you the 24 hr format, but what is getting converted to what is the question – V4Vendetta Dec 01 '11 at 07:06
-
@Piotr Auguscik when i use ToString() it returns 1:00:00 PM but i need 13:00:00 – nmathur Dec 01 '11 at 07:06
-
@nmathur check what bobbymcr has suggested it should work for you – V4Vendetta Dec 01 '11 at 07:11
-
@nmathur Check bobbymcr's answer, its all what you need. Your server must have defined enviroment variables telling him to convert `DateTime` to 12 hours format. So you have to change server setting or force the format you prefer everywhere where `tostring` is called – Piotr Auguscik Dec 01 '11 at 07:12
-
@phoog it is a String and when i convert it into DateTime AM or PM remains in Date. – nmathur Dec 01 '11 at 07:16
-
@Abdul i tried to Convert it to DateTime – nmathur Dec 01 '11 at 07:16
-
@V4Vendetta thanks pal, HH worked .. :) and thank you all for you suggestions. – nmathur Dec 01 '11 at 07:19
-
@nmathur It seems hard for you to tell what is the `Type` where the date and time are stored... Do you need to convert a `string` *"4 PM"* into another `string` "*16:00*" or do you want to print a `DateTime` in the format you want? – JiBéDoublevé Dec 01 '11 at 09:17
-
Try putting more effort into formulating your question. Use formatting, give examples, etc. – markus Dec 01 '11 at 13:23
10 Answers
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.

- 56,740
- 14
- 129
- 152
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);

- 23,769
- 3
- 56
- 67
Convert a string to a DateTime, you could try
DateTime timeValue = Convert.ToDateTime("01:00 PM");
Console.WriteLine(timeValue.ToString("HH:mm"));

- 525
- 1
- 8
- 18
You can use like this:
string date = "";
date = DateTime.ParseExact("01:00 PM" , "h:mm tt", CultureInfo.InvariantCulture).ToString("HH:mm");
h: 12 hours
mm: mins
tt: PM/AM
HH:24 hours

- 529
- 8
- 23
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");

- 29,856
- 10
- 92
- 122
Go through following code to convert the DateTime from 12 hrs to 24 hours.
string currentDateString = DateTime.Now.ToString("dd-MMM-yyyy h:mm tt");
DateTime currentDate = Convert.ToDateTime(currentDateString);
Console.WriteLine("String Current Date: " + currentDateString);
Console.WriteLine("Converted Date: " + currentDate.ToString("dd-MMM-yyyy HH:mm"));
Whenever you want the time should be displayed in24 hours use format "HH"
You can refer following link for further details: Custom Date and Time Format Strings

- 4,509
- 1
- 19
- 16
using System;
class Solution
{
static string timeConversion(string s)
{
return DateTime.Parse(s).ToString("HH:mm");
}
static void Main(string[] args)
{
Console.WriteLine(timeConversion("01:00 PM"));
Console.Read();
}
}

- 356
- 1
- 3
- 5
using System;
public class Program
{
public static string ConvertFrom12To24HoursFormat(string inputTime)
{
return DateTime.Parse(inputTime).ToString("hh:mm");
}
public static void Main(string[] args)
{
Console.WriteLine(ConvertFrom12To24HoursFormat("01:05 pm"));
}
}

- 5,040
- 16
- 42
- 49

- 1
- 1
You can use the ParseExact() method to obtain a DateTime object. You can then use the ToString() method of that DateTime object to convert the string to what ever you need as shown here.
You may use Cultures to parse and format your string, eg
var cultureSource = new CultureInfo("en-US", false);
var cultureDest = new CultureInfo("de-DE", false);
var source = "01:00 pm";
var dt = DateTime.Parse(source, cultureSource);
Console.WriteLine(dt.ToString("t", cultureDest));
This prints 13:00

- 2,346
- 23
- 39