简体   繁体   中英

DateTime.TryParseExact returns false

I'm trying to parse 4/27/2011 12:00:00 AM using M/d/yyyy H:m:sa pattern, invariant culture and default options but it doesn't parse.

I'll be very thankful if someone will help me to realize what's wrong.

Your pattern doesn't include tt , which is the AM/PM designator and is in your input text. Additionally, you want h for 12-hour clock rather than 24 for 24-hour clock, and it looks like you will always have two-digit minutes and seconds, so you probably just want a pattern of M/d/yyyy h:mm:ss tt .

Sample code which works:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        DateTime value;
        if (DateTime.TryParseExact("4/27/2011 12:00:00 AM",
                                   "M/d/yyyy h:mm:ss tt",
                                   CultureInfo.InvariantCulture,
                                   DateTimeStyles.None,
                                   out value))
        {
            Console.WriteLine(value);
        }
    }
}

See MSDN for more information on custom date and time format strings .

该模式是不正确的-没有a格式说明-它应该是tt

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