使用可为 null 的 DateTime

发布于 2024-11-15 18:32:35 字数 514 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一袭白衣梦中忆 2024-11-22 18:32:35

.ToShortDateString() 调用将其转换为字符串。您应该删除该调用。

另外,你说你已经用 javascript 确定了格式。如果用户没有 javascript 怎么办,您也应该进行服务器端检查。此外,由于它采用给定格式,因此您可以使用 DateTime.ParseExactDateTime.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 or DateTime.TryParseExact (so an invalid format doesn't throw an exception) since its more efficient. (The format string would be "yyyy-MM-dd") i believe.

疧_╮線 2024-11-22 18:32:35

不要使用“ToShortDateTimeString”将其转换为字符串,只需设置 Convert.ToDateTime 的结果:

myObject.ScoringLastUpgrade = Convert.ToDateTime(txtScoringUpgradeDate.Text);

假设您已经对 txtScoringUpgradeDate.Text 进行了足够的验证?

在处理这些类型转换时,我的偏好是使用 TryParse 方法,例如:

DateTime date;
if (DateTime.TryParse(txtScoringUpgradeDate.Text, out date))
    myObject.ScoringLastUpgrade = date;

Convert.ToDateTime,非常类似于显式的 DateTime.Parse当出现异常值时,将抛出 InvalidCastException。最好让你的代码具有容错能力,然后再捕获异常。

更新:根据您的最新评论:

在这种情况下,您不应返回 DateTime.MinValue,因为 MinValue 小于支持的最小值datetime 列。 CLR DateTime 支持小至 0000-01-01 的日期范围,而 SQL datetime(以及比较的 CLR SqlDateTime 类型)支持最小值 1753-01-01。由于它是可为空的 DateTime,因此您应该将其设置为 null:

public static DateTime? ToNullableDateTime(this string date)
{
    DateTime dateTime;
    return (DateTime.TryParse(date, out dateTime))
        ? (DateTime?)dateTime
        : null;
}

Don't convert it to string using 'ToShortDateTimeString', just set the result of Convert.ToDateTime:

myObject.ScoringLastUpgrade = Convert.ToDateTime(txtScoringUpgradeDate.Text);

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.:

DateTime date;
if (DateTime.TryParse(txtScoringUpgradeDate.Text, out date))
    myObject.ScoringLastUpgrade = date;

The Convert.ToDateTime, much like the explicit DateTime.Parse will throw an InvalidCastException 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, as MinValue is less than the supported min value of a datetime column. the CLR DateTime supports a date range down to 0000-01-01, whereas the SQL datetime (as well as the comparative CLR SqlDateTime type) supports a minimum value of 1753-01-01. As it as a nullable DateTime, you should set it to null:

public static DateTime? ToNullableDateTime(this string date)
{
    DateTime dateTime;
    return (DateTime.TryParse(date, out dateTime))
        ? (DateTime?)dateTime
        : null;
}
打小就很酷 2024-11-22 18:32:35

问题是您将 ToShortDateString() 放在最后,有效地将 DateTime 再次转换回 string 。尝试删除该行的那部分。

The problem is that you have put ToShortDateString() at the end there, effectively converting the DateTime back to a string again. Try removing that part of the line.

把昨日还给我 2024-11-22 18:32:35

就我而言(.cshtml):

<td>@item.InvoiceDate.Value.ToShortDateString().ToString()</td>

其中 InvoiceDtae 是数据库中的可为空日期

In my case (.cshtml):

<td>@item.InvoiceDate.Value.ToShortDateString().ToString()</td>

Where InvoiceDtae is Nullable Date in DB

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文