Why Can't Datetime.Parseexact() Parse the Am/Pm in "4/4/2010 4:20:00 Pm" Using "M'/'D'/'Yyyy H':'Mm':'Ss' 'Tt"

Why can’t DateTime.ParseExact() parse the AM/PM in “4/4/2010 4:20:00 PM” using “M'/'d'/'yyyy H':'mm':'ss' 'tt”

Make the hour format (H) lowercase like this:

DateTime.ParseExact(
"4/4/2010 4:20:00 PM",
"M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);

Uppercase "H" indicates 24-hour time and lowercase "h" indicates 12-hour time and will respect the AM/PM in the candidate string.

Side note: It is best to provide an instance of IFormatProvider to methods like this (even if it's just CultureInfo.InvariantCulture). It's one of those things that doesn't really matter until you hit problems with it so it can be good to be in the habit of specifying culture information.

Why can't DateTime.ParseExact() parse 9/1/2009 using M/d/yyyy

I suspect the problem is the slashes in the format string versus the ones in the data. That's a culture-sensitive date separator character in the format string, and the final argument being null means "use the current culture". If you either escape the slashes ("M'/'d'/'yyyy") or you specify CultureInfo.InvariantCulture, it will be okay.

If anyone's interested in reproducing this:

// Works
DateTime dt = DateTime.ParseExact("9/1/2009", "M'/'d'/'yyyy",
new CultureInfo("de-DE"));

// Works
DateTime dt = DateTime.ParseExact("9/1/2009", "M/d/yyyy",
new CultureInfo("en-US"));

// Works
DateTime dt = DateTime.ParseExact("9/1/2009", "M/d/yyyy",
CultureInfo.InvariantCulture);

// Fails
DateTime dt = DateTime.ParseExact("9/1/2009", "M/d/yyyy",
new CultureInfo("de-DE"));

Why can’t DateTime.ParseExact() parse the AM/PM in “4/4/2010 4:20:00 PM” using “M'/'d'/'yyyy H':'mm':'ss' 'tt”

Make the hour format (H) lowercase like this:

DateTime.ParseExact(
"4/4/2010 4:20:00 PM",
"M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);

Uppercase "H" indicates 24-hour time and lowercase "h" indicates 12-hour time and will respect the AM/PM in the candidate string.

Side note: It is best to provide an instance of IFormatProvider to methods like this (even if it's just CultureInfo.InvariantCulture). It's one of those things that doesn't really matter until you hit problems with it so it can be good to be in the habit of specifying culture information.

Error: String was not recognized as a valid DateTime in C#

The format string HH refers to 24-hour format and cannot be used in conjunction with tt. Use hh instead. This:

DateTime check = DateTime.ParseExact(recorded, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

works just fine.

How to allow datetime field to accept AM and PM

  1. Get the all DateTime in your query
  2. Use IF for your field
  3. Use syntax like

    If (Hours of Datetime > 12)
    Then
    Hours of Datetime - 12 + "PM"

I agree this is little bir confused but this is the structure you should follow.

Converting 2 strings to DateTime

string date = "2017-10-17";
string time = "8:00PM";

string startTime = $"{date} {time}";
DateTime myDate = DateTime.ParseExact(startTime, "yyyy-MM-dd h:mmtt",System.Globalization.CultureInfo.InvariantCulture);


Related Topics



Leave a reply



Submit