I am getting error in converting string to date. I don't want to use calendar control because date has to generate at runtime and textbox and not editable.
String was not recognized as a valid DateTime."
txtBoxRemediationDate.Text = System.DateTime.Now.ToString("d/m/yyyy");
_assessmentRemidationObject.RemediationRecommendedDate = Convert.ToDateTime(txtBoxRemediationDate.Text);
public DateTime RemediationRecommendedDate { get; set; }
答案 0 :(得分:1)
上面的代码目前将Month元素设置为分钟,因此:
txtBoxRemediationDate.Text = System.DateTime.Now.ToString("d/m/yyyy");
于2015年9月14日17:23将该值设为“14/23/2015”(无效日期)。
尝试将“m”更改为“M”:
txtBoxRemediationDate.Text = System.DateTime.Now.ToString("d/M/yyyy");
今天将值设置为格式正确的日期:“2015年9月9日”。
请点击此处查看更多格式:https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx