在godaddy上托管的asp.net中,字符串未被识别为有效的DateTime




我有一个应用程序,我需要从出生日期开始计算年份,我正在使用下拉列表填充日期、月份和年份。我已经在这里编码了年度下拉列表选择交换事件来计算年份。我的代码,当我在开发机器上执行时,它运行得很好,但当我在godaddy上托管它时,它会给我错误:

字符串未被识别为有效的DateTime。

我已经尝试了一些步骤,比如在把文化放进去,我有意见,但即使是那些不起作用的

    protected void DDLyear_SelectedIndexChanged(object sender, EventArgs e)
    {
        var date = DDLDay.Text + "/" + DDLMonth.Text + "/" + DDLYear.Text;
        DOB.Text = date;
        if (DDLDay.Text == "DD" || DDLMonth.Text == "MM" || DDLYear.Text == "YYYY")
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Date')</script>");
            DDLYear.Text = "YYYY";
            return;
        }
        if (Convert.ToDateTime(date) > System.DateTime.Now)
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('DOB cannot be greater then current date')</script>");
            //DOB.Text = System.DateTime.Now.ToString("dd/MM/yyyy");
            return;
        }
      //  DateTime now = System.DateTime.Now;
        //DateTime now = DateTime.Parse(System.DateTime.Now.ToString("yyyyMMdd"), System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
        //DateTime birthDate = Convert.ToDateTime(DOB.Text, System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
      DateTime now = DateTime.ParseExact(System.DateTime.Now.ToString(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
        string birthDate = Convert.ToDateTime(DOB.Text).ToString("dd/MM/yyyy");
        TimeSpan difference = Convert.ToDateTime(now) - Convert.ToDateTime(birthDate);
        //DateTime birthDate = Convert.ToDateTime(DOB.Text);
        //TimeSpan difference = Convert.ToDateTime(now) - Convert.ToDateTime(birthDate);
        int days = (int)difference.TotalDays;
        int year = days / 365;
        ////int remain = days % 365;
        ////int month = remain / 30;
        ////int day = remain % 30;
        txtage.Text = year + " " + "Years".ToString();
    }

堆栈跟踪

[FormatException: String was not recognized as a valid DateTime.]
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,  DateTimeStyles styles) +10973474
 System.Convert.ToDateTime(String value) +83     FAHIS_Data_Entry_Software_in_WEB.NewEnrolee5.DDLyear_SelectedIndexChanged(Object sender, EventArgs e) +397
 System.EventHandler.Invoke(Object sender, EventArgs e) +0
 System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +116
 System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent() +133
 System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +13
 System.Web.UI.Page.RaiseChangedEvents() +132
 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1644

用户文化信息

 cInfo = new CultureInfo("es-AR");
        DateTime DOB= Convert.ToDateTime(DOB.Text, cInfo.DateTimeFormat);

相关内容

  • 没有找到相关文章