I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

Getting error while parsing string to datetime

I have a datetime value in a CSV file and below is the sample data:

20/06/2016 11:52
21/06/2016 11:52
22/06/2016 11:52

Non when I try to parse this datetime, I get error:

String was not recognized as a valid DateTime.

I am not sure what will be the format of this date but I would always like to parse it in that culture in which my application will be using. So based on current culture I would like to parse my date.

This is how I am trying but getting error as above:

string row = "20/06/2016 11:52" 

Try 1:

CultureInfo culture = CultureInfo.CurrentCulture;
  DateTimeStyles styles = DateTimeStyles.None;
  DateTime dateValue;
  DateTime.TryParse(rowValue, culture, styles, out dateValue); // {1/1/0001 12:00:00 AM}

Try 2

 DateTimeFormatInfo usDtfi = new CultureInfo(culture.Name, false).DateTimeFormat;
  var l = Convert.ToDateTime(rowValue, usDtfi); //String was not recognized as a valid DateTime
  var g = DateTime.Parse(rowValue, usDtfi);//String was not recognized as a valid DateTime

All this above approches are failing and I would like to have exact date and want to store in my SQL Server database table.

My system datetime is in format : mm/dd/yy

I have already seen some questions like below:

String was not recognized as a valid DateTime " format dd/MM/yyyy"

Datetime format Issue: String was not recognized as a valid DateTime

But all those answers are specifying date format but I don't know what will be the format; that is why I am trying to detect from current culture. I am not sure whether I am thinking in a right way.

Upvotes: 0

Views: 2010

Answers (2)

tinstaafl
tinstaafl

Reputation: 6958

If you're sure that each string you encounter will be in a proper format, but you just don't know which one, one thing you can do is get an array of all the different formats on your machine and use that in the parse exact method:

var formats = (from CultureInfo ct in CultureInfo.GetCultures(CultureTypes.AllCultures)
               select ct.DateTimeFormat.GetAllDateTimePatterns()).SelectMany((x) => x).ToArray();
DateTime test = DateTime.ParseExact("20/06/2016 11:52", formats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal);

That code on my machine generates over 26,000 formats. As long the string follows one of them it will be accepted.

If getting the proper format will be inconsistent you could go this route:

var formats = (from CultureInfo ct in CultureInfo.GetCultures(CultureTypes.AllCultures)
               select ct.DateTimeFormat);
string dateString = "20/06/2016 11:52";
DateTime temp = new DateTime(0);
foreach (DateTimeFormatInfo dfi in formats)
{
    if (DateTime.TryParseExact(dateString, dfi.GetAllDateTimePatterns(), dfi, DateTimeStyles.None, out temp))
    {
        break;
    }
}
if(temp == new DateTime(0))
{
    //save string to get it's format
}

Upvotes: 1

Hypnobrew
Hypnobrew

Reputation: 1140

If you are 100% sure the format will always be the same, you can use the method ParseExact like:

var parsedDate = DateTime.ParseExact(row, "dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);

Upvotes: 1

Related Questions