我有以下代码
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。
答案 0 :(得分:7)
简短回答:MMMM
不等于MMM
。
旁注:使用CultureInfo.InvariantCulture
解析此类字符串只能识别英文名称,它似乎会在使用其他语言的机器上失败。