DateTime.Parse fails for today (01 mar 2012)! o

Today I got a really strange problem.

Try to execute this C# code:

class Program
{
    static void Main(string[] args)
    {

        string yesterdayString = (DateTime.Now - TimeSpan.FromDays(1)).ToString("R");
        string nowString = DateTime.Now.ToString("R");

        DateTime.Parse(yesterdayString);
        DateTime.Parse(nowString);

        DateTime.Parse("Wed, 29 Feb 2012 18:05:49 GMT"); // this is what i have in yesterdayString
        DateTime.Parse("Thu, 01 Mar 2012 18:05:40 GMT"); // this is what i have in nowString
    }
}

Of course you will get the problem just for today. I've added the last two commands to let you know what the problem is. Tomorrow and throughout the month of March DateTime.Parse will throw a FormatException (String was not recognized as a valid DateTime.)

Why?


I would say you found a bug. This should work:

// 
var spanish = CultureInfo.GetCultureInfo("es");
var date = DateTime.Parse(DateTime.Now.ToString("R", spanish), spanish);

or for Italian it still throws...

var italian = CultureInfo.GetCultureInfo("it");
var date = DateTime.Parse(DateTime.Now.ToString("R", italian), italian);

This is confirmed to be a bug.

If you need more info on this topic, please look at http://connect.microsoft.com/VisualStudio/feedback/details/822536/datetime-parse-result-in-exception-when-the-format-is-rfc1123-pattern-and-locale-is-a-latin-one

链接地址: http://www.djcxy.com/p/58002.html

上一篇: 从模块外部采购Puppet文件

下一篇: DateTime.Parse今天失败(2012年3月1日)! Ø