28

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?

Zanon
  • 29,231
  • 20
  • 113
  • 126
nmathur
  • 423
  • 1
  • 4
  • 6

10 Answers10

39

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.

Marco
  • 56,740
  • 14
  • 129
  • 152
19

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);
bobbymcr
  • 23,769
  • 3
  • 56
  • 67
14

Convert a string to a DateTime, you could try

    DateTime timeValue = Convert.ToDateTime("01:00 PM");
    Console.WriteLine(timeValue.ToString("HH:mm"));
Pratyush
  • 525
  • 1
  • 8
  • 18
6

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

Praveen Gopal
  • 529
  • 8
  • 23
6
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");
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
6

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

Shivkant
  • 4,509
  • 1
  • 19
  • 16
1
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();
    }
}
Atno
  • 356
  • 1
  • 3
  • 5
0
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"));
    }
}
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
0

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.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
-1

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

okrumnow
  • 2,346
  • 23
  • 39