我正在使用 Visual Studio,我想将文本框中的字符串转换为 DateTime
格式。我正在使用函数 Convert.ToDateTime()
,但返回的值是最小值 (0/0/0001 00:00:00
)。
问题是什么?
我从文本框中检索字符串的代码。
//pass startdate end date to studentResult.aspx
Session["startdate"] = txtStartDate.Text.ToString();
Session["enddate"] = txtEndDate.Text.ToString();
我将字符串转换为日期时间格式的代码。
string startdate = (string)(Session["startdate"]);
string enddate = (string)(Session["enddate"]);
DateTime one = Convert.ToDateTime(startdate);
DateTime two = Convert.ToDateTime(enddate);
最佳答案
正如我的 comment 中所指出的, Convert.ToDateTime
方法返回 DateTime.MinValue
如果参数为null
。
Return Value
Type: System.DateTime
The date and time equivalent of the value of value, or the date and time equivalent of DateTime.MinValue if value is null.
您的参数可能为 null
,但由于您没有向我们提供更多详细信息,因此我们无法知道确切的问题是什么。
作为一个常见错误,请确保您将 TextBox
的 .Text
属性作为参数传递,而不是其本身。参见鱿鱼的comment .
关于c# - 将字符串转换为日期时间返回最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17540542/