DateTime.TryParseExact似乎无法使用"tt"匹配AM/PM

Aar*_*ryn 7 .net c# datetime

C#noob在这里.简单的问题,但在网上查看所有内容,我似乎正在这样做.但任何人都可以告诉我为什么这段代码不起作用:

string testDateString = "2/02/2011 3:04:01 PM";
string testFormat = "d/MM/yyyy h:mm:ss tt";
DateTime testDate = new DateTime();
DateTime.TryParseExact(testDateString, testFormat, null, 0, out testDate);

// Value of testDate is the default {1/01/0001 12:00:00 a.m.}
Run Code Online (Sandbox Code Playgroud)

但只是从字符串日期删除AM/PM和格式中的"tt"按预期工作?

string testDateString = "2/02/2011 3:04:01";
string testFormat = "d/MM/yyyy h:mm:ss";
DateTime testDate = new DateTime();
DateTime.TryParseExact(testDateString, testFormat, null, 0, out testDate);

// Value of testDate is the expected {2/02/2011 3:04:01 a.m.}
Run Code Online (Sandbox Code Playgroud)

jev*_*lio 8

尽管它的确切名称,DateTime.TryParseExact()取决于系统文化来解析日期.尝试指定使用AM/PM表示法的区域性,例如en-US:

DateTime.TryParseExact(testDateString, testFormat, new System.Globalization.CultureInfo("en-US"), DateTimeStyles.None, out testDate);
Run Code Online (Sandbox Code Playgroud)