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.
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.
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)".)