English 中文(简体)
在C#中为系统可更改的日期(最新时间?)确定一个短日期
原标题:Get short date for System Nullable datetime (datetime ?) in C#

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

www.un.org/Depts/DGACM/index_french.htm www.un.org/spanish/ga/president

页: 1 ToShortDateString available.

最佳回答

页: 1 数值首先(从中删除)。

var shortString = yourDate.Value.ToShortDateString();

但也检查了<代码>your 日期:有价值:

if (yourDate.HasValue) {
   var shortString = yourDate.Value.ToShortDateString();
}
问题回答

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

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

Demo

If the DateTime? is null this returns an empty string.

请注意:

如希望保证有显示价值,可与<代码>一起使用/code>。 ToShortDateString methods that other poste similar this:

yourDate.GetValueOrDefault().ToShortDateString();

如果价值确实无效,则将显示01/01/0001。

Check if it has value, then get required date

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

提 出

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

如果你使用......cshtml,你可以使用

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

或者如果你试图在C#中找到行动或方法上的短日期,那么

yourDate.GetValueOrDefault().ToShortDateString();

以上是Steve。

我也赞同我的项目中所采用的做法。 罚款。 谢谢。

A more concise way to do the same thing would be var shortString = yourDate?.Date; which would return a null if it is a null and the short date if it is not null.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签