- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下问题。
在我声明的类中:
vulnerabilityDetailsTable.AddCell(new PdfPCell(new Phrase(currentVuln.Published.ToString(), _fontNormale)) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 30, PaddingTop = 10 });
有趣的部分是:currentVuln.Published.ToString()。这工作正常。
Published 是一个声明为nullable 的DateTime 属性,以这种方式:
public System.DateTime? Published { get; set; }
问题是,在以前的方式中,currentVuln.Published.ToString() 的打印值类似于 18/07/2014 00:00:00(时间也包含在日期中)。
我只想显示日期而不显示时间,所以我尝试使用类似的东西:
currentVuln.Published.ToShortDateString()
但它不起作用,我在 Visual Studio 中收到以下错误消息:
Error 4 'System.Nullable<System.DateTime>' does not contain a definition for 'ToShortDateString' and no extension method 'ToShortDateString' accepting a first argument of type 'System.Nullable<System.DateTime>' could be found (are you missing a using directive or an assembly reference?) C:\Develop\EarlyWarning\public\Implementazione\Ver2\PdfReport\PdfVulnerability.cs 93 101 PdfReport
这似乎是因为我的 DateTime 字段可以为空。
我错过了什么?我该如何解决这个问题?
最佳答案
你是对的,这是因为你的 DateTime
字段可为空。
DateTime
的扩展方法不适用于 DateTime?
, 但要理解为什么,你必须意识到实际上没有 DateTime?
类。
最常见的是,我们使用 ?
编写可空类型语法,如 DateTime?
, int?
,等等,就像你上面所做的那样。但这只是syntactic sugar对于 Nullable<DateTime>
, Nullable<int>
等
public Nullable<DateTime> Published { get; set; }
所有那些明显分开的Nullable
类型来自单个 generic Nullable<T>
struct包装您的类型并提供两个有用的属性:
HasValue
(用于测试底层包装类型是否有值),以及Value
(为了访问该基础值,假设有一个)首先检查以确保有一个值,然后使用 Value
属性访问基础类型(在本例中为 DateTime
),以及通常可用于该类型的任何方法。
if (currentVuln.Published.HasValue)
{
// not sure what you're doing with it, so I'll just assign it...
var shortDate = currentVuln.Published.Value.ToShortDateString();
}
对于 C# 6.0 及更高版本,您可以使用 null conditional而不是 .Value
:
var shortDate = currentVuln.Published?.ToShortDateString();
关于c# - 在可为空的 DateTime 对象上使用 ToShortDateString() 方法时出现一些问题,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25060876/
我用vb写了这段代码: expireDate = (DateTime.Parse(decal.DecalExpireDate).ToShortDateString).ToString 现在我尝试用 C
tentative_date 具有有效日期,但当我尝试转换为 shortdate 时,显示空字符串。 @if (Model.bookings.tentative_date.HasValue) {
这是给你的一个奇怪的。 我们有一个 c# 界面,从年初开始就可以在 Windows XP(32 位)PC 上正常运行。我们刚刚使用 SCCM 安装的应用程序将 PC 升级到 Windows 7(64
能否请您告诉我如何解决下面的查询。 LINQ 将我的日期格式化为写出的示例:2015 年 8 月 8 日。我需要 8/8/2015。我尝试的每一次尝试都会给我不同的错误信息。我究竟做错了什么。 var
我在代码中有一个类似这样的属性: public Nullable WorkDate 所以我尝试在代码中的某个地方使用 ToShortDateString 但它甚至没有在 VS IDE 中显示这样的方法
我们有一个具有这种结构的数据库: public partial class ChartData { public int Id { get; set; } public DateTim
我有一个必须在编辑 View 中显示的日期时间字段 @Html.EditorFor( model => model.StartDate ) 我怎么能用这样的东西; @Html.EditorFor( m
我需要改变格式 this.TextBox3.Text = DateTime.Now.ToShortDateString(); 所以它返回(例如)25.02.2012 ,但我需要 02.25.2012
我想知道为什么 .NET 可移植类库中没有 .ToShortDateString。我有 2 个使用相同代码的项目(Silverlight 和常规 .NET 类库),代码涉及在 DateTime 对象上
我知道我可以使用格式字符串,但我不想丢失日期/时间格式的文化特定表示。例如 2011 年 5 月 4 日 |下午 2:06 | ... 应该是 05/04/2011 |下午02:06 | ... 但是
当我已经从 linq 带回一个日期并将其转换为短日期字符串时,我正在尝试使用 linq 按日期排序: var NOVs = from n in db.CT_NOVs join i in db.CT_I
我像这样使用 d.PODate.ToString() 来显示(见图片)我可以使用检查元素看到它...我只想显示日期而不是时间,当我尝试使用 d.PODate.ToString("d") 它给了我这样的
我需要在使用 lambda 表达式的 linq 查询中调用 ToShortDateString: toRet.Notification = Repositories .portalDb.por
我有以下问题。 在我声明的类中: vulnerabilityDetailsTable.AddCell(new PdfPCell(new Phrase(currentVuln.Published.ToS
我想使用带有隐式运算符的泛型类。问题是使用底层函数。我认为最好的描述是我的代码 public class AnnotationField { public T Value { get; set
我有这样的日历: 查看 .cs文件 protected void Page_Load(object sender, EventArgs e) {
在 VS 2015 中创建通用应用程序并尝试在共享项目中为 DateTime 使用 .ToShortDateString() 方法时出现以下问题。Visual Studio 2015 智能感知将此显示
这条线 TitleDate = string.Format("Date: {0, -10} Title: {1, 10}" ,c.Date.ToShortDateString() ,c.Title)
我是一名优秀的程序员,十分优秀!