0

I would like to parse a datetime string with a special format.

Unfortunately I get an error. The datetime string is like the following: "08012020 21:17:05".

I tried the following code sample:

if (DateTime.TryParse(strDateTime, out dtDateTime)) Print(strDateTime + " --> " + dtDateTime); else Print("Unable to parse DateTime " + strDateTime );

Any ideas how to parse the special format?

Gerik

Gerik
  • 29
  • 2
  • 5
    Does this answer your question? [datetime.parse and making it work with a specific format](https://stackoverflow.com/questions/341175/datetime-parse-and-making-it-work-with-a-specific-format) ... which is second goolge resutl for the title of your question – Selvin Jan 11 '20 at 15:28

1 Answers1

5

You need to use DateTime.TryParseExact or DateTime.ParseExact methods to specific exact format if a your string is not a standard date format of your CurrentCulture settings.

The "proper" format seems like ddMMyyyy HH:mm:ss with a "proper" culture like InvariantCulture as;

if (DateTime.TryParseExact(strDateTime, "ddMMyyyy HH:mm:ss", CultureInfo.InvarianCulture, 
                           DateTimeStyles.None, out dtDateTime))
{
   // Print
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364