from string to datetime

J

juli

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!
 
C

Cor Ligthert

juli,

Please don't call my name in this newsgroup.
However I thought that this was the answer on your question.
\\\
string[] str = {"11/02/04","11:23:00","AM"};
string mystring = String.Join(" ",str);
///
I hope this helps?

Cor
 
J

Jon Skeet [C# MVP]

juli said:
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);
}
}
 
W

W.G. Ryan eMVP

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 said:
juli said:
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);
}
}
 
W

W.G. Ryan eMVP

Cor - with all due respect - this approach is a little challenged.

The main thing is that the concatenated value your method creates is still a
string - it will have to be parsed to be a DateTime like she needs. More
importantly though - there's no validation on those values the way you're
doing it - so if the three values were Juli, Bill, Cor instead of the
dates - your method would work fine. You need a Parse to ensure this
doesn't happen - and in real apps - I'd parse each piece up front to make
sure that I knew my final conversion would be solid - if even one paramater
was junk - I'm not going to be able to do much of anything.
 
C

Cor Ligthert

Bill,

This is a post which is in a kind of continuing of some other post.
However there was datetime variable in the post and I readed that as a
string this time, probably because I had already answered with a sample in
two earlier post how to make of a datetime variable from a string.

However I understand why you wrote this, in this earlier posts was (str was
dt1)

DateTime dta = Convert.ToDateTime(str,null);

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

In my opinion it is this a lot nicer code than any concationation or
stringbuilder loop.
(While you know that I am one of the last who avoid loops)

However just my thought,

Cor
 
C

Cor Ligthert

Bill,

I saw there was an typo in my message.
\\
string[] str = {"11/02/04","11:23:00","AM"};
string mystring = String.Join(" ",str);
DateTime dta = Convert.ToDateTime(mystring,null);
///

This gives me the change to tell you something about dates what happened in
the VB language group and you will be probably very happy with the end
conclussion.

Me poor had a discussion with 3 real dotNet MVP's. And not a short one and
very deep. My point with this is always culture independent date
conversions. However those MVP's told that I did not understand it.

And than at a certain moment there came a message from somebody from
Australia, who told, Cor I agree with you. That is the reason that we have
changed all our US software for European.

:)))

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

Top