简体   繁体   English

没有DateTime?.ToString(string)重载?

[英]No DateTime?.ToString(string) overload?

I am aware of the standard procedure for displaying a DateTime in a custom format, like so: 我知道以自定义格式显示DateTime的标准过程,如下所示:

MessageBox.Show(dateSent.ToString("dd/MM/yyyy hh:mm:ss"));

However, when I change the variable from a DateTime to a DateTime? 但是,当我将变量从DateTime更改为DateTime? to accept null values, I lose the definition for the ToString(string) overload. 为了接受空值,我失去了ToString(string)重载的定义。 I need to use DateTime? 我需要使用DateTime? as I am reading from a database which potentially has null values - if the field in the database has a null value, then I need to assign the variable a null value too. 当我从一个可能具有空值的数据库中读取时 - 如果数据库中的字段具有空值,那么我也需要为变量赋值空值。

So I have two questions: 所以我有两个问题:

1) Out of curiosity, does anyone know if there is a reason why DateTime? 1)出于好奇,有没有人知道DateTime?是否有原因DateTime? does not contain an overload for ToString(string) ? 不包含ToString(string)的重载?

2) Could anyone suggest an alternative method for what I am trying to achieve? 2)有人可以为我想要实现的目标建议一种替代方法吗?

DateTime? is syntactic sugar for Nullable<DateTime> and that's why it don't have ToString(format) overload. Nullable<DateTime>语法糖,这就是它没有ToString(format)重载的原因。

However, you can access underlying DateTime struct using Value property. 但是,您可以使用Value属性访问基础DateTime结构。 But before that use HasValue to check, if the value exists. 但在此之前使用HasValue检查,如果值存在。

MessageBox.Show(dateSent.HasValue ? dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") : string.Empty)

Instead of having to manually perform a null check every time, you can write an extension method. 您可以编写扩展方法,而不必每次都手动执行空检查。

 public static string ToStringFormat(this DateTime? dt, string format)
 {
      if(dt.HasValue) 
         return dt.Value.ToString(format);
      else
         return "";
 }

And use it like this (with whatever string format you want) 并像这样使用它(使用你想要的任何字符串格式)

 Console.WriteLine(myNullableDateTime.ToStringFormat("dd/MM/yyyy hh:mm:ss"));

你仍然可以使用

variableName.Value.ToString(customFormat);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 格式化DateTime错误“方法'ToString'没有重载需要1个参数” - formatting DateTime error “No overload for method 'ToString' takes 1 arguments” 使用 Linq to Entities 实现 DateTime.ToString(string) - Implementing DateTime.ToString(string) with Linq to Entities DateTime.ToString格式化并将结果字符串反转回datetime - DateTime.ToString formatting and reversing the resulting string back to the datetime 获取&#39;DateTime.ToString()&#39;以输出与&#39;DateTime&#39;的XML序列化相同的字符串 - Getting 'DateTime.ToString()' to output the same string as an XML serialization of a 'DateTime' ToString重载中未处理的异常 - Unhandled exception in ToString overload 试图合成DateTime? 变量导致“方法ToString没有重载需要1个参数错误” - Trying to formate DateTime? variable causes “No overload for method ToString takes 1 argument error” 在.NET中发出字符串格式为“M”的DateTime.ToString - Issue DateTime.ToString with string format “M” in .NET `XmlConvert.ToDateTime(String)`和`XmlConvert.ToString(DateTime)`输出不一致 - `XmlConvert.ToDateTime(String)` and `XmlConvert.ToString(DateTime)` outputs are inconsistent 方法'tostring'没有重载需要1个参数 - No overload for method 'tostring' takes 1 arguments 方法“ToString”没有重载需要1个参数 - no overload for method “ToString” takes 1 arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM