How to convert DateTime? to DateTime

C#.NetDatetime

C# Problem Overview


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

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

I have attempted the following:

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

C# Solutions


Solution 1 - C#

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;

Solution 2 - C#

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.

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

Solution 3 - C#

Try this

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

Solution 4 - C#

You can use a simple cast:

DateTime dtValue = (DateTime) dtNullAbleSource;

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

Solution 5 - C#

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

Assuming that UpdatedDate is DateTime?, then this should work:

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

To make the code a bit easier to read, you could use the HasValue property instead of the null check:

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

This can be then made even more concise:

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

Solution 6 - C#

How about the following:

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

Solution 7 - C#

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

memDateLogin = m.memDateLogin ?? DateTime.MinValue

Solution 8 - C#

You can also try Nullable(T) Properties:

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

Solution 9 - C#

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

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

Solution 10 - C#

Try this:

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

Solution 11 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWaheedView Question on Stackoverflow
Solution 1 - C#chills42View Answer on Stackoverflow
Solution 2 - C#JoshView Answer on Stackoverflow
Solution 3 - C#Valentin VView Answer on Stackoverflow
Solution 4 - C#César LeónView Answer on Stackoverflow
Solution 5 - C#adrianbanksView Answer on Stackoverflow
Solution 6 - C#Simon WilsonView Answer on Stackoverflow
Solution 7 - C#Ravi RamView Answer on Stackoverflow
Solution 8 - C#Mateusz RogulskiView Answer on Stackoverflow
Solution 9 - C#Sadiqabbas HiraniView Answer on Stackoverflow
Solution 10 - C#Tim S. Van HarenView Answer on Stackoverflow
Solution 11 - C#Harsh VyasView Answer on Stackoverflow