Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 648
Paolo Sanchi Avatar asked Mar 01 '12 17:03

Paolo Sanchi


1 Answers

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);
like image 173
2 revs Avatar answered Sep 23 '22 20:09

2 revs