[英]String was not recognized as a valid DateTime in asp.net, hosted on godaddy
我有一个需要从出生日期获得年份的应用程序,我正在使用下拉列表填充日,月和年。 我已经在year dropdownlist selectindexchange事件上进行了编码,以在这里计算我的代码年,当我在开发机器上执行该代码时,它可以完美运行,但是当我将其托管在godaddy上时,它会给我错误:
无法将字符串识别为有效的DateTime。
我已经尝试了一些步骤来放入我有评论的cultureinfo,但即使这些都不起作用
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.