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.WriteLi开发者_开发技巧ne(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
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);
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.
精彩评论