今天(2012年3月1日)DateTime.Parse失败! o_0 [英] DateTime.Parse fails for today (01 mar 2012)! o_0

查看:82
本文介绍了今天(2012年3月1日)DateTime.Parse失败! o_0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我遇到了一个非常奇怪的问题.

Today I got a really strange problem.

尝试执行此C#代码:

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
    }
}

当然,您今天会遇到问题.我添加了最后两个命令,以使您知道问题出在哪里. 明天,整个三月,DateTime.Parse将抛出FormatException(字符串未被识别为有效的DateTime.)

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.)

为什么?

推荐答案

我会说您发现了一个错误.这应该起作用:

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);

这篇关于今天(2012年3月1日)DateTime.Parse失败! o_0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆