在 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
Get short date for System Nullable datetime (datetime ?) in C#
提问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 ToShortDateString
available.
我没有看到ToShortDateString
可用的。
采纳答案by Darren
You need to use .Value
first (Since it's nullable).
您需要先使用.Value
(因为它可以为空)。
var shortString = yourDate.Value.ToShortDateString();
But also check that yourDate
has a value:
但还要检查它yourDate
是否具有值:
if (yourDate.HasValue) {
var shortString = yourDate.Value.ToShortDateString();
}
回答by JNYRanger
That function is absolutely available within the DateTime
class. 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 Nullable
is a generic on top of the DateTime
class you will need to use the .Value
property of the DateTime?
instance to call the underlying class methods as seen below:
由于Nullable
是DateTime
类之上的泛型,因此您需要使用实例的.Value
属性DateTime?
来调用底层类方法,如下所示:
DateTime? date;
String shortDateString;
shortDateString = date.Value.ToShortDateString();
Just be aware that if you attempt this while date
is 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 ToShortDateString
method 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);
If the DateTime?
is null
this returns an empty string.
如果是,DateTime?
则null
返回空字符串。
Note that the "d" custom format specifieris identical to 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.
我已经分享了我在我的项目中使用的这个。它工作正常。谢谢你。