DateTime.ParseExact - 英国日期和时间

时间:2013-01-22 12:32:08

标签: c# datetime

我正在尝试解析以下英国格式DateTime字符串:24/01/2013 22:00

但是,我一直收到这个错误:

  

字符串未被识别为有效的DateTime。

CultureInfo.CurrentCulture返回“en-GB”,这是正确的

这是我的代码

    [TestMethod]
    public void TestDateTimeParse()
    {
        DateTime tester = DateTime.ParseExact("24/01/2013 22:00", "d/M/yyyy hh:mm", CultureInfo.CurrentCulture);

        int hours = tester.Hour;
        int minutes = tester.Minute;

        Assert.IsTrue(true);
    }

3 个答案:

答案 0 :(得分:16)

hh是12小时制。您应该使用HH代替。

DateTime.ParseExact("24/01/2013 22:00", 
                    "d/M/yyyy HH:mm", // <-- here
                    CultureInfo.CurrentCulture)

答案 1 :(得分:3)

"hh"用于小时,使用12小时制01至12

"HH"用于小时,使用从00到23的24小时制

尝试这样;

public static void Main(string[] args)
{
    DateTime tester = DateTime.ParseExact("24/01/2013 22:00", "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
}

这是DEMO

您也可以从MSDN查看Custom Date and Time Format Strings

答案 2 :(得分:1)

你的格式错了,试试这个:

DateTime tester = DateTime.ParseExact("24/01/2013 22:00", "dd/MM/yyyy HH:mm", CultureInfo.CurrentCulture);
相关问题