-5

I have data that comes as string in dd/mm/yyyy format. How to parse this string to datetime in c#?

I tried DateTime.TryParse(), but it doesn't recognize the string as valid date.

user1176058
  • 642
  • 1
  • 8
  • 21

1 Answers1

5

Use:

DateTime.ParseExact(yourString, "dd/MM/yyyy", CultureInfo.InvariantCulture)

or:

DateTime.TryParseExact(yourString, "dd/MM/yyyy",
    CultureInfo.InvariantCulture, DateTimeStyles.None, out yourResult)

(The method DateTime.TryParse that you tried, can work if the format provider (culture info) used has this format, day/month/year, with slashes and the correct order, as its "default" date pattern. This happens for example if you use the new CultureInfo("en-GB"), "English (United Kingdom)".)

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181