开发者

c# DateTime object error

开发者 https://www.devze.com 2023-01-20 01:10 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消