简体   繁体   中英

DateTime.TryParseExact doesn't seem to be recognizing chinese AM and PM designator characters within a string

I'm trying to parse date and time strings that contain either Chinese AM or PM characters as below. For some reason, the DateTime.TryParse method can get at the correct date and time, but when I try to use the DateTime.TryParseExact method, with what looks to me to be a correct format specifier/mask, the parse fails as shown below where I'm left with a default value for the dateExact variable.

Here's the code:

var dateString = "2018/2/9 下午 03:55:17";

DateTime date = default(DateTime);

DateTime dateExact = default(DateTime);

// this works
DateTime.TryParse(dateString, new CultureInfo("zh-CHS"), DateTimeStyles.None, out date);

// this doesn't work
DateTime.TryParseExact(dateString, "yyyy/M/d tt HH:mm:ss", new CultureInfo("zh-CHS"), DateTimeStyles.None, out dateExact);

Console.WriteLine("Date: " + date);

Console.WriteLine("DateExact: " + dateExact);

And here's the output:

Date: 2/9/2018 3:55:17 PM

DateExact: 1/1/0001 12:00:00 AM

"HH" denotes an hour in 24-hour format. While you're indicating the time is in the PM, 03:55:17 in 24-hour format must be in the AM

The following worked fine for me:

var dateString = "2018/2/9 下午 03:55:17";
DateTime dateExact = default(DateTime);
DateTime.TryParseExact(dateString, "yyyy/M/d tt hh:mm:ss", new System.Globalization.CultureInfo("zh-CHS"), System.Globalization.DateTimeStyles.None, out dateExact);
System.Console.Write(dateExact.ToString());

Note the usage of "hh" instead of "HH"

This has nothing to do with Chinese dates as far as I can tell, the following fails to parse:

var dateString = "2018/2/9 PM 03:55:17";
DateTime dateExact = default(DateTime);
var result = DateTime.TryParseExact(dateString, "yyyy/M/d tt HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dateExact);

System.Console.WriteLine(result.ToString());
System.Console.WriteLine(dateExact.ToString());

while changing the string to "2018/2/9 AM 03:55:17" (or "2018/2/9 PM 15:55:17") will cause it to succeed

DateTime.TryParseExact doesn't seem to be recognizing chinese AM and PM designator characters within a string

Your designator is not the issue as it matches exactly the .NET Framework's definition of a PM Designator for the "zh-CHS" culture:

new CultureInfo("zh-CHS").DateTimeFormat.PMDesignator == "下午" // returns true

Therefore, the problem lies elsewhere in your format string:

Your input has a 12 hour time but you used " HH " which represents the hour as a number from 00 through 23. Use " hh " instead.

DateTime.TryParseExact(dateString, "yyyy/M/d tt hh:mm:ss",
   new CultureInfo("zh-CHS"), DateTimeStyles.None, out dateExact);

Date: 09/02/2018 15:55:17

DateExact: 09/02/2018 15:55:17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM