I'm trying to parse 4/27/2011 12:00:00 AM
using M/d/yyyy H:m:s a
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With