datetime convertion error

J

juli

Good afternoon!
I have an error while trying to convert to DateTime ,here is the code
and the error:

ArrayList al = new ArrayList();
temp_str=Line.Split(' ');
al.Add(temp_str[0]); // value="11/02/2004"
al.Add(temp_str[1]); // value="10:53:10"
temp_str2=(string[])al.ToArray(typeof(string));

temp = String.Join(" ",temp_str2);



Now I am trying to do:
date=Convert.ToDateTime(temp);
or
date=DateTime.Parse(temp);
and gives me such error during the debugging: An unhandled exception
of type 'System.FormatException' occurred in mscorlib.dll

Additional information: String was not recognized as a valid DateTime.

Can anyone explain me why? Thank you very much.
 
C

Cor Ligthert

Juli,

In seperated post I did show you something else than you show now.
(While others did show you other methods which are as well not the same as
you show now)

To keep it by the one I did, that was this, (I combined my last post by all
the previouw post when Bill wrote something about that.)

\\
string[] str = {"11/02/04","11:23:00","AM"};
string mystring = String.Join(" ",str);
DateTime dta = Convert.ToDateTime(str,null);
///
date=Convert.ToDateTime(temp);

The least you did not do in your post now is setting that second parameter
of the Convert.ToDateTime to "null", which means: set it to the setting of
the standard lanuguage and country setting of the computer.

I am not so known with the rules in this newsgroups, however in other dotnet
newsgroup it is normal to keep your messages in the same thread until it
changes completly.

Cor
 
J

juli jul

I am not sure that my last post been posted so I am sorry if I post the
same question twice:
My problem in convertion is that I am trying to convert an American
format of date : 01/25/2004 and the convertion fails unless I use the
Europe type of convertion: 25/01/2004.
Is there a parameter that allows to convert date in American format?
Thank you very much !
 
C

Cor Ligthert

Juli,

Yes your original post is there and has two sample answers.
One from Jon Skeet and one from me..

Although I never like it to make dates computer setting independent, here my
sample fixed on the US culture.

DateTime dta = Convert.ToDateTime(mystring,
new System.Globalization.CultureInfo("en-US"));

(I saw there was a typo about mystring/str in my previous answer by the way)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top