在 C# 中获取 System Nullable datetime (datetime ?) 的短日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18982303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:47:36  来源:igfitidea点击:

Get short date for System Nullable datetime (datetime ?) in C#

c#.netstringdatetimenullable

提问by user2811143

How to get short date for Get short date for System Nullable datetime (datetime ?)

如何获得短日期 获得短日期 System Nullable datetime (datetime ?)

for ed 12/31/2013 12:00:00--> only should return 12/31/2013.

for ed 12/31/2013 12:00:00--> 只应返回12/31/2013.

I don't see the ToShortDateStringavailable.

我没有看到ToShortDateString可用的。

采纳答案by Darren

You need to use .Valuefirst (Since it's nullable).

您需要先使用.Value(因为它可以为空)。

var shortString = yourDate.Value.ToShortDateString();

But also check that yourDatehas a value:

但还要检查它yourDate是否具有值:

if (yourDate.HasValue) {
   var shortString = yourDate.Value.ToShortDateString();
}

回答by JNYRanger

That function is absolutely available within the DateTimeclass. Please refer to the MSDN documentation for the class: http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx

该函数在DateTime类中绝对可用。请参阅该类的 MSDN 文档:http: //msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx

Since Nullableis a generic on top of the DateTimeclass you will need to use the .Valueproperty of the DateTime?instance to call the underlying class methods as seen below:

由于NullableDateTime类之上的泛型,因此您需要使用实例的.Value属性DateTime?来调用底层类方法,如下所示:

DateTime? date;
String shortDateString;
shortDateString = date.Value.ToShortDateString();

Just be aware that if you attempt this while dateis null an exception will be thrown.

请注意,如果您尝试此操作 whiledate为 null 将引发异常。

回答by Muhammad Umar

Check if it has value, then get required date

检查它是否有价值,然后获取所需的日期

if (nullDate.HasValue)
{
     nullDate.Value.ToShortDateString();
}

回答by Steve Danner

If you want to be guaranteed to have a value to display, you can use GetValueOrDefault()in conjunction with the ToShortDateStringmethod that other postelike this:

如果你想保证有一个值显示,你可以GetValueOrDefault()结合ToShortDateString其他postelike这样的方法使用:

yourDate.GetValueOrDefault().ToShortDateString();

This will show 01/01/0001 if the value happened to be null.

如果该值碰巧为空,这将显示 01/01/0001。

回答by Tim Schmelter

string.Format("{0:d}", dt);works:

string.Format("{0:d}", dt);作品:

DateTime? dt = (DateTime?)DateTime.Now;
string dateToday = string.Format("{0:d}", dt);

Demo

演示

If the DateTime?is nullthis returns an empty string.

如果是,DateTime?null返回空字符串。

Note that the "d" custom format specifieris identical to ToShortDateString.

请注意,“d”自定义格式说明符ToShortDateString.

回答by user6721009

Try

尝试

    if (nullDate.HasValue)
    {
         nullDate.Value.ToShortDateString();
    }

回答by Er. Binod Mehta

If you are using .cshtml then you can use as

如果你使用 .cshtml 那么你可以使用

<td>@(item.InvoiceDate==null?"":DateTime.Parse(item.YourDate.ToString()).ToShortDateString())</td>

or if you try to find short date in action or method in c# then

或者,如果您尝试在 c# 中查找操作或方法中的短日期,则

yourDate.GetValueOrDefault().ToShortDateString();

And is already answered above by Steve.

史蒂夫已经在上面回答了。

I have shared this as i used in my project. it works fine. Thank you.

我已经分享了我在我的项目中使用的这个。它工作正常。谢谢你。