How to convert string array to datetime list ?

Chocolade 536 Reputation points
2022-02-03T20:37:56.28+00:00

In the top of form1I have a List type DateTime

private List<DateTime> dates = new List<DateTime>();  

at some point in my code I save the dates in the List to a text file

dates = rad.dates;  
                            StreamWriter w = new StreamWriter(@"d:\Downloaded Images\Dates\dates.txt");  
                            int i;  
                            for (i = 0; i < dates.Count - 1; i++)  
                            {  
                                w.WriteLine(dates[i]);  
                            }  
                            w.Write(dates[i]);  
                            w.Close();  

The result is a text file with dates like this

171181-dates2.jpg

After saving the dates to the text file I want to read back the dates to the dates list in the constructor
I read lit like this

string[] dates = File.ReadAllLines(@"d:\Downloaded Images\Dates\dates.txt");  

The problem is that it's string array type and not DateTime list and I also want to assign it to the existing DateTime dates List and not to a new variable.

Developer technologies Windows Forms
Developer technologies C#
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-02-03T20:58:07.637+00:00

    using linq its:

    var dates = File.ReadAllLines(@"d:\Downloaded Images\Dates\dates.txt").Select(d => DateTime.Parse(d)).ToArray();

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.