Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I am trying to add some datetime values into array but I am getting an error.
C#
DateTime[] date = {"10:09", "10:01","10:02","10:03","10:04", "10:05","10:06","10:07","10:08","10:09", "10:10"};


How do I add the Time or Date values to the DateTime array.

I know that these values can be stored using string array, but I need help with DateTime.

Thanks in Advance.

Eswar
Posted
Updated 9-Dec-16 9:41am
v5
Comments
Dalek Dave 4-Nov-10 5:00am    
Edited for Grammar, Syntax and Spelling.

This will work.
Create first an array of string.
Then use Array.ConvertAll() function.
string[] array = { "10:09", "10:01", "10:02", "10:03", "10:04", "10:05", "10:06", "10:07", "10:08", "10:09", "10:10" };
            DateTime[] dtArray = Array.ConvertAll(array, o => 
                {
                    DateTime d = Convert.ToDateTime(o);
                    return (DateTime)d;
                     
                }).ToArray();


Please read my explanations in the article:


Delegates in C# - attempt to look inside. Part 4


Good luck.
 
Share this answer
 
try this :
C#
string[] date = { "10:09", "10:01", "10:02", "10:03", "10:04", "10:05", "10:06", "10:07", "10:08", "10:09", "10:10" };
            System.Globalization.DateTimeFormatInfo info = new System.Globalization.DateTimeFormatInfo();
            info.ShortDatePattern = "HH:mm";
            DateTime[] dates = new DateTime[100];
            for (int i = 0; i < date.Length; i++)
            {
                dates[i] = DateTime.Parse(date[i], info);
            }
 
Share this answer
 
v2
Comments
Philippe Mori 10-Dec-16 11:04am    
Well, hard-code 100 here is really bad. If you convert an array to another one, then you should use the length of the first array for the size of the second one.
hi Eswa, i have found two links that might be of your help :

http://dotnetperls.com/datetime-array

and this :
http://www.csharp-examples.net/string-format-datetime/

i will try to find more links on this.
 
Share this answer
 
Comments
TweakBird 4-Nov-10 4:40am    
ok thank you tarun. in this way also we can try. already i seen those links. any thanks for posting.
Tarun.K.S 4-Nov-10 4:53am    
ohkay your welcome eswa!
Hi,

you can maybe try "List" and after you convert to array


List<DateTime> date;

....

DateTime [] adate = date.ToArray()

jerem
 
Share this answer
 
Comments
TweakBird 4-Nov-10 4:25am    
Let me check
TweakBird 4-Nov-10 4:39am    
Good useful answer..thanks
Dalek Dave 4-Nov-10 5:01am    
Good Call!
hi.
you can use of :
C#
DateTime[] date = { Convert.ToDateTime("10:09"), Convert.ToDateTime("10:01"),Convert.ToDateTime( "10:02"), Convert.ToDateTime("10:03"), Convert.ToDateTime("10:04"), Convert.ToDateTime("10:05"), Convert.ToDateTime("10:06"), Convert.ToDateTime("10:07"), Convert.ToDateTime("10:08"), Convert.ToDateTime("10:09"), Convert.ToDateTime("10:10" )};
 
Share this answer
 
Comments
Philippe Mori 10-Dec-16 11:10am    
Read about DRY: Don't repeat yourself
C#
DateTime[] myArray = new DateTime[]{ DateTime.Parse("06:45"), DateTime.Parse("08:30") };
 
Share this answer
 
Comments
CHill60 9-Dec-16 20:12pm    
You had so many clues to suggest that posting a solution might *not* be a "Good Idea"...
1. The question is over 6 years old (the OP has probably *not* waited this long for an answer)
2. The subject line says "Solved"
3. The question has an "Accepted Answer"...the OP has acknowledged that their question has been answered.
4. Your solution is number 6 and doesn't really add anything to solutions 1 through 5

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