我无法将从ddl中提取的字符串值转换为DateTime数据类型。
错误讯息
类型' System.FormatException'的例外情况发生在mscorlib.dll中 但未在用户代码中处理。附加信息:字符串是 未被识别为有效的DateTime。
我只需要在SQL数据库中更新日期。
这是DataDictionary的图像,我要求的属性是生日
SQL Table http://i65.tinypic.com/ri4z95.jpg
这些是我当前获取并转换日期时间的代码。
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
... **(Write your content here)**
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date);
Console.WriteLine("Year: {0}, Month: {1}, Day: {2}", dt.Year, dt.Month, dt.Day);
答案 1 :(得分:0)
您可以使用TryParse来安全,而不是ParseExact。即:
var ddlDaySelectedItemValue = "29";
var ddlMonthSelectedItemValue = "Feb"; // February, 2, 02
var ddlYearSelectedItemValue = "2016";
DateTime birthDate;
string birth = ddlYearSelectedItemValue + " " + ddlMonthSelectedItemValue + " " + ddlDaySelectedItemValue;
if (DateTime.TryParse(birth, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out birthDate))
{
objCustomer.birthDate = birthDate;
}
答案 2 :(得分:-2)
将您的代码更改为:
DateTime.ParseExact(birth, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);