C# - Get date difference of Nullable DateTime in int

前端 未结 2 678
悲&欢浪女
悲&欢浪女 2021-01-29 03:23

I have two DateTime variables of Nullable types.

DateTime? date1 = new DateTime();
DateTime? date2 = new DateTime();

I need to get the differe

2条回答
  •  再見小時候
    2021-01-29 03:41

    If your goal is to return null when one (or both) of the dates is null, you can write like this (if you can use ? operator which is available in C# 6.0 compiler):

    int? days = (int?) (date1 - date2)?.TotalDays;
    

提交回复
热议问题