使用可为 null 的 DateTime
我正在使用 LINQ 并且有一些属性,即 DateTime?类型。
如果我现在想从文本框中添加值,我似乎无法让它工作。
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ScoringLastUpgrade", DbType="Date")]
public System.Nullable<System.DateTime> ScoringLastUpgrade
我使用的文本框已使用 javascript 确保格式为“2011-06-17”,
但现在当我尝试执行此操作时:
myObject.ScoringLastUpgrade = Convert.ToDateTime(txtScoringUpgradeDate.Text).ToShortDateString();
我收到此错误:“无法将类型字符串转换为 DateTime?”
如何做到这一点?
I am using LINQ and have a few properties thats DateTime? type.
If i now want to add the value from a textbox i cant seem to get this to work.
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ScoringLastUpgrade", DbType="Date")]
public System.Nullable<System.DateTime> ScoringLastUpgrade
The textbox i use i have made sure with javascript that the format will be '2011-06-17'
But now when i try to do this:
myObject.ScoringLastUpgrade = Convert.ToDateTime(txtScoringUpgradeDate.Text).ToShortDateString();
I get this error: "Cannot convert type string to DateTime?"
How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
.ToShortDateString()
调用将其转换为字符串。您应该删除该调用。另外,你说你已经用 javascript 确定了格式。如果用户没有 javascript 怎么办,您也应该进行服务器端检查。此外,由于它采用给定格式,因此您可以使用
DateTime.ParseExact
或DateTime.TryParseExact
(因此无效格式不会引发异常),因为它的效率更高。 (格式字符串将是“yyyy-MM-dd”
)我相信。The
.ToShortDateString()
call is converting it into a string. You should remove that call.Also, you say you've made sure of the format with javascript. What if the user doesn't have javascript, you should do server-side checks too. Also, since it's in a given format, you can use
DateTime.ParseExact
orDateTime.TryParseExact
(so an invalid format doesn't throw an exception) since its more efficient. (The format string would be"yyyy-MM-dd"
) i believe.不要使用“ToShortDateTimeString”将其转换为字符串,只需设置
Convert.ToDateTime
的结果:假设您已经对
txtScoringUpgradeDate.Text
进行了足够的验证?在处理这些类型转换时,我的偏好是使用 TryParse 方法,例如:
Convert.ToDateTime,非常类似于显式的 DateTime.Parse当出现异常值时,将抛出
InvalidCastException
。最好让你的代码具有容错能力,然后再捕获异常。更新:根据您的最新评论:
在这种情况下,您不应返回
DateTime.MinValue
,因为MinValue
小于支持的最小值datetime
列。 CLRDateTime
支持小至 0000-01-01 的日期范围,而 SQLdatetime
(以及比较的 CLRSqlDateTime
类型)支持最小值 1753-01-01。由于它是可为空的DateTime
,因此您应该将其设置为 null:Don't convert it to string using 'ToShortDateTimeString', just set the result of
Convert.ToDateTime
:Assuming you've done sufficient validation on
txtScoringUpgradeDate.Text
?My preference when dealing with these type conversions is to use the
TryParse
method, e.g.:The
Convert.ToDateTime
, much like the explicitDateTime.Parse
will throw anInvalidCastException
when an excepional value occurs. It's better to make your code fault tollerant then needlesly catch an exception.UPDATE: based on your last comment:
You shouldn't return
DateTime.MinValue
in this case, asMinValue
is less than the supported min value of adatetime
column. the CLRDateTime
supports a date range down to 0000-01-01, whereas the SQLdatetime
(as well as the comparative CLRSqlDateTime
type) supports a minimum value of 1753-01-01. As it as a nullableDateTime
, you should set it to null:问题是您将
ToShortDateString()
放在最后,有效地将DateTime
再次转换回string
。尝试删除该行的那部分。The problem is that you have put
ToShortDateString()
at the end there, effectively converting theDateTime
back to astring
again. Try removing that part of the line.就我而言(.cshtml):
其中 InvoiceDtae 是数据库中的可为空日期
In my case (.cshtml):
Where InvoiceDtae is Nullable Date in DB