我从文件中读取了两种不同格式的日期时间字符串:
19/02/2019 08:24:59
2/17/2019 12:25:46 PM
对于第一个格式,我编写的自定义格式字符串如下:
string firstDate = "19/02/2019 08:24:59";
string customFormatForFirstDateTimeString = "dd/mm/yyyy hh:mm:ss";
我按照以下方式使用它:
string firstResultingDateAndTime;
bool parsingSuccessful = DateTime.TryParseExact(
firstDate,
customFormatForFirstDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out firstResultingDateAndTime);
问题在于
parsingSuccessful
的结果为false
。
对于第二个日期时间字符串,代码如下:string secondDate = "2/17/2019 12:25:46 PM";
string customFormatForSecondDateTimeString = "m/dd/yyy hh:mm:ss PM";
string secondResultingDateAndTime;
parsingSuccessful = DateTime.TryParseExact(
secondDate,
customFormatForSecondDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out secondResultingDateAndTime);
此处,我也收到。
parsingSuccessful == false;
我认为自定义格式字符串不适用于日期时间字符串,但我没能弄清楚为什么。请帮忙解决。非常感谢。