John:
I'm liking your post the best -but I just saw the other two responses and
figured I'd throw in what I aleady had:
I'd make a function out of it so when it comes up again - I have it there:
private DateTime GetDateFromStringArray(System.String[] dateParts)
{
//Should probably validate the inputs here to make sure
//you only have good data to begin with otherwise - throw exception
System.String ReturnValue = null;
for(System.Int16 x = 0; x < dateParts.Length; x++)
{
ReturnValue += dateParts[x] + " ";
}
return DateTime.Parse(ReturnValue);
}
Here's a simple call - I'd also throw in some verification logic to make
sure each piece is a valid time component - otherwise we may get the whole
way to then end and then fail.
I do in fact like your approach - just figured I'd chime in to make things
interesting.
--
W.G. Ryan MVP (Windows Embedded)
TiBA Solutions
www.tibasolutions.com |
www.devbuzz.com |
www.knowdotnet.com
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
juli <ji****@gmail.com> wrote: Hello dear Cor or anyone around!:)
This didn't help me(convert) : I have 3 strings in an array :
str[0]='11/02/04' ,str[1]='11:23:00" and str[2]=AM. How do I convert
str[0]+str[1]+str[2] to a proper datetime variable. Thanks a lot!
Try this:
using System;
using System.Globalization;
class Test
{
static void Main()
{
string[] bits = {"11/02/04", "11:23:00", "AM"};
string combined = bits[0]+" "+bits[1]+" "+bits[2];
Console.WriteLine (combined);
DateTime dt = DateTime.ParseExact (combined,
"dd/MM/yy hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine (dt);
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too