0

I am trying to parse a date into a list of objects with 2 strings from a text file. I have a string than Date and than another string in the text file on the same line.

Like the following

"Adam-15/02/2000-Likes pasta"

I tried doing the following to get the DateTime but i keep getting the error

String was not recognized as a valid DateTime.

My code

List<ChildDetails> children = new List<ChildDetails>();
string line;     
System.IO.StreamReader file new System.IO.StreamReader(@"C:\Users\tomik\Desktop\School Y2 T2\Application Development\LAB3\\WindowsFormsApplication1\BodkinVanHorn.docx");   
while ((line = file.ReadLine()) != null)
{
    string[] words = line.Split('-');
    children.Add(newChildDetails(words[0],DateTime.ParseExact(words[1],"dd/mm/yyyy", null), words[2]));
}
Igor
  • 60,821
  • 10
  • 100
  • 175
T.Ryan
  • 41
  • 6
  • As a side note I would recommend enclosing the `StreamReader` in a `using` block to ensure it is closed/disposed after you are done using it. – Igor Feb 18 '19 at 22:27

2 Answers2

2

mm = minutes, MM = months. The proper format string in this case should be "dd/MM/yyyy". The casing in the format string is important.

See also Custom Date and Time Format Strings

Igor
  • 60,821
  • 10
  • 100
  • 175
  • @T.Ryan - if the error is the same than what you are passing into `DateTime.ParseExact` is *not* the value you posted in your sample data in your post. Likely the value of a line in your file is something else than you expect. Time to debug your code and your file to figure out why. Use the IDE and attach the debugger, break on exceptions, and inspect the values when the Exception occurs. – Igor Feb 19 '19 at 16:08
  • So when i use date.parse it works but when i use date.parseExact it gives me that error. – T.Ryan Feb 19 '19 at 18:10
0

Your code should look like this:

var children = new List<ChildDetails>();
string line;   

try {  
    using (StreamReader file = new StreamReader(@"C:\Users\tomik\Desktop\School Y2 T2\Application Development\LAB3\\WindowsFormsApplication1\BodkinVanHorn.docx");   
    {
        while ((line = file.ReadLine()) != null)
        {
            string[] words = line.Split('-');
            children.Add(newChildDetails(words[0], DateTime.ParseExact(words[1],"dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None), words[2]));
        }
    }
} 
catch (Exception ex) 
{
    // Catch some issue
    Console.WriteLine("The file could not be read: maybe doesnt exists");
}

Here's a good read on CultureInfo.InvariantCulture

What does CultureInfo.InvariantCulture mean?

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
  • It doesn't throw the error anymore but it doesn't display the data either – T.Ryan Feb 19 '19 at 15:18
  • When i get rid of the date and just use the 2 strings just to test if adding the items works i get an error saying index was outside the boundary of the array – T.Ryan Feb 19 '19 at 15:29
  • Can you debug and see what does line contains and post the same here. Like place a breakpoint on this line: string[] words = line.Split('-'); – Gauravsa Feb 19 '19 at 21:56