Last post Jan 21, 2008 08:13 PM by kingkyser
Jan 21, 2008 06:42 PM|kingkyser|LINK
I need to convert strings of the form #### to DateTime type. What is the most straight forward way to do this? I'm sure it's easy. I just haven't been able to find what I am looking for by Googling (maybe I need to work on my Googling skills). There are
no colons in the military time string. Always just 4 numbers.
This is what I current have:
t.Time = Convert.ToDateTime(txtDate.Text + " " + txtTime.Text);
Example of txtDate: 01/11/2008
Example of txtTime: 0537
Jan 21, 2008 06:54 PM|Ken Tucker|LINK
I would use dateTime.ParseExact to convert the string
Jan 21, 2008 07:07 PM|Joseph Ghassan|LINK
*Delete this post*
Jan 21, 2008 07:10 PM|kingkyser|LINK
I had to change "New" to "new" and remove Globalization. Visual Studio didn't like Globalization there.
I'm getting this error now:
System.FormatException: The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
t.Time = DateTime.ParseExact(txtDate.Text + " " + txtTime.Text, "dd/MM/yyyy HHmm", new CultureInfo("en-US"));
Jan 21, 2008 07:14 PM|kingkyser|LINK
Thank you for your advice. I am well aware that I should mark an answer as an answer. I haven't done so yet because I experienced problems with his answer. Thus, I'm not going to mark his post as the answer.
Jan 21, 2008 08:13 PM|kingkyser|LINK
I figured it out. This is what I ended up with:
DateTime dt = DateTime.ParseExact(txtDate.Text + " " + txtTime.Text, "MM/dd/yyyy HHmm", new CultureInfo(0x0409, true));
It took me a while to realize that I needed the MM and dd inverted. I'm not sure that I needed to specify new CultureInfo like that, but it worked. Thanks for the input Ken!