If you know the format in which the date strings appear in the CSV file, you should be able to parse them using ParseExact
.
Be sure to specify the exact format. In the ParseExact example you commented, you're supplying "dd-MMM-yyyy hh:mm:ss" as a format, but that would never work on dates that do not include a timestamp.
Assuming the two possible formats you've mentioned, this should work:
DateTime.ParseExact(
dateString,
new string[] { "dd/MM/yyyy", "dd-MMM-yyyy" },
CultureInfo.InvariantCulture,
DateTimeStyles.None);
PS: I prefer using TryParseExact
by the way, to prevent an exception from being raised. That would look something like this:
if (!DateTime.TryParseExact(
dateString,
new string[] { "dd/MM/yyyy", "dd-MMM-yyyy" },
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out DateTime parsedDateTime))
{
// parsing has failed, you could now
// - throw an exception
// - assign a default value to the date time field
// - ...
}
// parsedDateTime can be used here