Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi
I have the following list of dates :

01/01/2008
03/01/2008
04/01/2008
07/01/2008
08/01/2008
09/01/2008

I'm reading this from a text file into a array of type string by using split() function on a new line.I than need to convert this array of string dates into an array of actual dates but I seem to be "stuck" at this point.

Please see below :

C#
StreamReader sr = new StreamReader("C:\\Users\\temp\\readtest.txt");


line = sr.ReadToEnd();

string[] commaSeparator = new string[] { "\n" };

string[] results = line.Split(commaSeparator, StringSplitOptions.None);
                
                
foreach (string s in results)
       {
        DateTime results2 = Convert.ToDateTime(s);
         //Console.WriteLine(s);
        } 


Could someone please assist.I do not need the answer as such but just to be placed on the right path.

Thanks
Posted
Updated 13-Aug-14 3:57am
v2
Comments
[no name] 13-Aug-14 10:05am    
Assist with what? What is wrong with what you have? What does "stuck" mean?
Herman<T>.Instance 13-Aug-14 10:12am    
Convert might fail
[no name] 13-Aug-14 10:24am    
Yes it might but he does not tell us what his actual problem is. He could be getting a file not found error for all we know.
Herman<T>.Instance 13-Aug-14 10:38am    
true

1 solution

Read all about DateTime.TryParse[^] method.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Aug-14 12:16pm    
5ed, but I would question OP: why did you end up with the array of those string in first place? I would rather review the design of the code. Using strings representing data instead of data itself is a big nasty mistakes, typical for the beginners these days.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900