DateTime.TryParseExact()返回false

时间:2014-10-26 09:03:36

标签: c# datetime tryparse

我有以下代码

public static void Main()
{
    DateTime D = new DateTime();
    D = DateTime.Now;

    string s1 = D.ToString("ddMMMMyyyy");
    Console.WriteLine(s1);

    Console.WriteLine(DateTime.TryParseExact(s1, "ddMMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out D));
    Console.ReadKey();
}

有人可以帮我意识到我正在犯的明显错误。

我将DateTime.Now转换为自定义格式的字符串并尝试将其转换回来,但TryParseExact返回false。

1 个答案:

答案 0 :(得分:7)

简短回答:MMMM不等于MMM

旁注:使用CultureInfo.InvariantCulture解析此类字符串只能识别英文名称,它似乎会在使用其他语言的机器上失败。

相关问题