C# 如何转换日期时间?到日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1091870/
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-06 08:01:19  来源:igfitidea点击:

How to convert DateTime? to DateTime

c#.netdatetime

提问by Waheed

I want to convert a nullable DateTime (DateTime?) to a DateTime, but I am getting an error:

我想将可为空的 DateTime ( DateTime?)转换为 a DateTime,但出现错误:

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

无法隐式转换类型“System.DateTime?” 到'System.DateTime'。存在显式转换(您是否缺少演员表?)

I have attempted the following:

我尝试了以下方法:

DateTime UpdatedTime = (DateTime)_objHotelPackageOrder.UpdatedDate == null 
    ? DateTime.Now : _objHotelPackageOrder.UpdatedDate;

采纳答案by chills42

You want to use the null-coalescing operator, which is designed for exactly this purpose.

您想使用空合并运算符,它正是为此目的而设计的。

Using it you end up with this code.

使用它你最终得到这个代码。

DateTime UpdatedTime = _objHotelPackageOrder.UpdatedDate ?? DateTime.Now;

回答by adrianbanks

You need to call the Valueproperty of the nullable DateTime. This will return a DateTime.

您需要调用可为空的 DateTime的Value属性。这将返回一个日期时间。

Assuming that UpdatedDateis DateTime?, then this should work:

假设UpdatedDateDateTime?,那么这应该有效:

DateTime UpdatedTime = (DateTime)_objHotelPackageOrder.UpdatedDate == null ? DateTime.Now : _objHotelPackageOrder.UpdatedDate.Value;

To make the code a bit easier to read, you could use the HasValueproperty instead of the nullcheck:

为了使代码更易于阅读,您可以使用HasValue属性而不是null检查:

DateTime UpdatedTime = _objHotelPackageOrder.UpdatedDate.HasValue
                          ? _objHotelPackageOrder.UpdatedDate.Value
                          : DateTime.Now;

This can be then made even more concise:

这可以变得更加简洁:

DateTime UpdatedTime = _objHotelPackageOrder.UpdatedDate ?? DateTime.Now;

回答by Tim S. Van Haren

Try this:

尝试这个:

DateTime UpdatedTime = (DateTime)_objHotelPackageOrder.UpdatedDate == null ? DateTime.Now : _objHotelPackageOrder.UpdatedDate.Value;

回答by Simon Wilson

How about the following:

以下情况如何:

DateTime UpdatedTime = _objHotelPackageOrder.UpdatedDate.HasValue ? _objHotelPackageOrder.UpdatedDate.value : DateTime.Now;

回答by Valentin Vasilyev

Try this

尝试这个

DateTime UpdatedTime = _objHotelPackageOrder.UpdatedDate ?? DateTime.Now;

回答by Josh

MS already made a method for this, so you dont have to use the null coalescing operator. No difference in functionality, but it is easier for non-experts to get what is happening at a glance.

MS 已经为此制定了一种方法,因此您不必使用空合并运算符。功能上没有区别,但非专家更容易一目了然地了解正在发生的事情。

DateTime updatedTime = _objHotelPackageOrder.UpdatedDate.GetValueOrDefault(DateTime.Now);

回答by Ravi Ram

Here is a snippet I used within a Presenter filling a view with a Nullable Date/Time

这是我在演示者中使用的一个片段,用可空的日期/时间填充视图

memDateLogin = m.memDateLogin ?? DateTime.MinValue

回答by Mateusz Rogulski

You can also try Nullable(T) Properties:

您还可以尝试Nullable(T) 属性

DateTime UpdatedTime = _objHotelPackageOrder.UpdatedDate.HasValue 
    ? DateTime.Now : _objHotelPackageOrder.UpdatedDate.Value;

回答by Sadiqabbas Hirani

Consider using the following which its far better than the accepted answer

考虑使用以下比公认的答案要好得多的内容

DateTime UpdatedTime = _objHotelPackageOrder.UpdatedDate == null 
    ? DateTime.Now : (DateTime)_objHotelPackageOrder.UpdatedDate;

回答by César León

You can use a simple cast:

您可以使用简单的演员表:

DateTime dtValue = (DateTime) dtNullAbleSource;

As Leandro Tupone said, you have to check if the var is null before

正如 Leandro Tupone 所说,你必须先检查 var 是否为空