繁体   English   中英

DateTime.Parse或Convert.ToDateTime不起作用

[英]DateTime.Parse or Convert.ToDateTime is not working

我有一个带有日历扩展程序的文本框。

格式应如下所示:

<add key="DateFormat" value="dd/MM/yyyy"/>

我的aspx标记上有以下内容

  <asp:TextBox ID="txt" runat="server" 
                        meta:resourcekey="txt" MaxLength="150" HtmlEncode="False"></asp:TextBox>
                        <ajaxToolkit:CalendarExtender runat="server"
                            TargetControlID="txt"
                                            PopupButtonID="Image1" Format="<%$Appsettings:DateFormat%>" />

为什么我尝试在这样的属性中使用它:

datett= DateTime.Parse(txt.Text),

它说FormatException。

我调试并尝试了Convert.ToDatetime,引发了同样的异常。

我正在测试的文字是30/05/2015

根据我的web.config中的格式应该可以正常工作。

update1我使用以下代码根据用户选择来更改页面的语言和文化,这也许就是为什么它失败的原因,

我看到很多答案,第二个问题是,如何获得当前的文化?

/// <summary>
        /// Handles the AcquireRequestState event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            //Create culture info object 
            /*var ci = new CultureInfo(Session["Language"].ToString());
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);*/

            System.Web.UI.Page p = (System.Web.HttpContext.Current.Handler as System.Web.UI.Page);
            if (p != null)
            {
                p.UICulture = p.Culture = new CultureInfo((string)Session["Language"]).Name;
            }
        }

解析时应添加格式化程序。 向Web.config添加DateFormat密钥本身不会做任何事情。

VehicleDesctructionDate = DateTime.Parse(TxtVehicleDestructionDateReturnedVehicle.Text , /* put formatter here */,null);

在所有情况下,最好使用TryParse处理任何格式错误。

您可以使用DateTime.ParseExact和/或显式传递区域性:

var enCulture = new System.Globalization.CultureInfo("en-us");
DateTime result = DateTime.ParseExact("30/05/2015", 
                                      "dd/MM/yyyy", 
                                       enCulture );

编辑 :如果您要动态更改区域性并将其存储在会话中,则应该可以使用:

var userCulture = new System.Globalization.CultureInfo((string)Session["Language"]);
DateTime result = DateTime.Parse(TxtVehicleDestructionDateReturnedVehicle.Text, userCulture );

解决方案是,如果您在IIS上托管网站,或者在本地工作,则应在IIS上调整区域性属性,在解析日期时应使用以下代码:

DateTime.Parse(date,new CultureInfo("en-GB",false));

如果这对您有效,则可以在母版页上将此文化设置为默认设置。

另一个解决方案是拆分日期文本,并将每个拆分后的部分放入相应的日期部分,例如对于此日期“ 30/05/2015”:

        string x = "30/05/2015";
    DateTime dt = new DateTime(int.Parse(x.Split('/')[2]), int.Parse(x.Split('/')[1]), int.Parse(x.Split('/')[0]));

尝试使用

Convert.ToDateTime(TxtVehicleDestructionDateReturnedVehicle.Text, CultureInfo.GetCultureInfo("en-GB"));

要么

DateTime.Parse(TxtVehicleDestructionDateReturnedVehicle.Text , Appsettings.DateFormat,null);
Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM