ObjectDataSource控件无法解析字符串日期时间 [英] ObjectDataSource fails to parse string to DateTime

查看:169
本文介绍了ObjectDataSource控件无法解析字符串日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有值的文本框,存储ValidFrom形式值:

I have a textbox with value that stores ValidFrom form value:

31.01.2012

和文化设置为:

<globalization culture="en-GB" uiCulture="en-GB"/>

在web.config中。

in web.config.

而现在,ObjectDataSource控件更新方法:

And now, ObjectDataSource update method:

    public static void UpdateLac(int id, DateTime ValidFrom)
    {
        /// ...
    }

失败,因为我得到异常的字符串不能被解析。但是日期格式的 DD.MM.YYYY 31.01.2012 )是有效的EN-GB格式,可以被解析(据我所知)。我曾与下面code测试了它:

fails as I get exception that string cannot be parsed. However date in format dd.mm.yyyy (31.01.2012) is valid en-GB format and can be parsed (as far as I know). I have tested it with following code:

            DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat;
            var date = DateTime.Parse("31.01.2012", dtfi);
            Console.Write(date.ToLongDateString());

那么怎么来了,是ObjectDataSource控件内部转换失败的字符串(31.01.2012)转换为日期时间在这个例子吗?

So how come that ObjectDataSource internal conversion fails to convert string (31.01.2012) to DateTime in this example?

推荐答案

据我了解文化信息是从操作系统直接加载(在这种情况下窗户),您可以检查指定的格式的区域设置。这是我的电脑截图:

As far as I know the culture info is loaded directly from the OS ( in this case windows), you can check on your regional setting for the format specified. This is a screenshot from my pc:

http://imageshack.us/photo/my-images/96 /engbg.png/

正如你所看到的短日期格式为:DD / MM / AA,所以也许有一些与您的服务器的区域设置或输入应该是怎么回事:31 / 01 / 12而不是31.01.2012

As you can see the format for short date is: dd/MM/aa, so maybe there is something going on with your server regional settings or the input should be: 31/01/12 instead of 31.01.2012

希望这有助于。

这篇关于ObjectDataSource控件无法解析字符串日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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