0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • Maybe because there are no seconds. Use TryParseExact – Tim Schmelter Jan 04 '17 at 15:02
  • 1
    Duplicate http://stackoverflow.com/questions/341175/datetime-parse-and-making-it-work-with-a-specific-format – Rabid Penguin Jan 04 '17 at 15:04
  • Is the date you're parsing stored in your file in various formats? So you could have multiple dates in the file, each with a different format? – Rabid Penguin Jan 04 '17 at 15:12
  • Take a look at the following method it supports multiple date formats for parsing. DateTime.ParseExact Method (String, String[], IFormatProvider, DateTimeStyles) https://msdn.microsoft.com/en-us/library/332de853(v=vs.110).aspx – Wagner DosAnjos Jan 04 '17 at 15:12
  • @Rabid penguin:yes the date can have multiple formats so I want datetime to be stored based on my current culture – I Love Stackoverflow Jan 04 '17 at 15:44
  • I'm not sure you answered the question :p So the datetime stored in the file should all be the same format; your current cultures format? Or they are all different formats and you want to parse them TO the current culture? – Rabid Penguin Jan 04 '17 at 15:48
  • @Rabid penguin irrespective of what the formats of datetime is I just want to store datetime in my current culture. – I Love Stackoverflow Jan 04 '17 at 16:03
  • If each date in the text file could be in a different format you'll need to have the format information in the text file too for each date. Otherwise how would you tell MM/dd apart from dd/MM? – Rabid Penguin Jan 04 '17 at 16:16
  • @Rabid penguin ok you have a point.what if 1 file contains all datetime in dd/mm/yy format and another file contains mm/dd/yy format.still this is not possible?? – I Love Stackoverflow Jan 04 '17 at 16:20
  • That would be possible because now you know the format ahead of time and could use parseexact – Rabid Penguin Jan 04 '17 at 16:22

2 Answers2

1

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);
Hypnobrew
  • 1,120
  • 9
  • 23
1

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
}
tinstaafl
  • 6,908
  • 2
  • 15
  • 22