Marthin
Marthin

Reputation: 6543

c# DateTime object error

I trying to create a DateTime object. I get my values from a string array.

When i run the code, the exact date time is printed as : 2009-12-21 06:07:05

string tmpDate = values[0].Trim() +" "+ values[1].Trim();

Console.WriteLine(tmpDate);

DateTime time = DateTime.ParseExact(tmpDate, "yyyy-MM-dd HH:mm:ss", null);

For some reason i get a DateTime format exception for this? Any ide´s?

Best regards Marthin

Upvotes: 0

Views: 103

Answers (2)

Hans Passant
Hans Passant

Reputation: 941545

When things that obviously should work don't work then assume there's something you cannot see. Like this:

        string[] values = new string[] { "2009-12-21\0", "06:07:05" };
        string tmpDate = values[0].Trim() + " " + values[1].Trim();
        Console.WriteLine(tmpDate);
        DateTime time = DateTime.ParseExact(tmpDate, "yyyy-MM-dd HH:mm:ss", null);

Upvotes: 1

Andy
Andy

Reputation: 856

Try specifying something other than null for the 3rd parameter (needs to implement IFormatProvider). Since it rarely matters in the applications I build, I often use DateTimeFormatInfo.InvariantInfo or CultureInfo.InvariantCulture.

Upvotes: 0

Related Questions