C# 如何检查DateTime是否为空

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

How to check if DateTime is null

c#datetimenullcalendar

提问by user1202765

I have my two DateTimeas follow:

我有我的两个DateTime如下:

        DateTime startDate = CalendarFrom.SelectedDate;
        DateTime endDate = CalendarTo.SelectedDate;

Now, I want to check if the startDateand endDateis selected or not. As I know, we cannot assign null to DateTimeso I write in this way:

现在,我想检查startDateendDate是否被选中。据我所知,我们不能为 null 赋值,DateTime所以我这样写:

       if (startDate == DateTime.MinValue && endDate == DateTime.MinValue)
       {
             // actions here
       }

However I realise that for the endDate, it is not "1/1/0001 12:00:00 AM" so it cannot be checked by using DateTime.MinValue.

但是我意识到对于endDate,它不是“1/1/0001 12:00:00 AM”,因此无法使用DateTime.MinValue.

I would like to know how will I be able to do checking for the endDate. Thanks!

我想知道如何检查endDate. 谢谢!

回答by Jon Skeet

A DateTimeitself can't be null - it's cleanest to use DateTime?aka Nullable<DateTime>to represent nullable values.

ADateTime本身不能为空 - 使用DateTime?akaNullable<DateTime>来表示可空值是最干净的。

It's not clear what CalendarFromand CalendarToare, but they may not support Nullable<DateTime>themselves - but it would be a good idea to centralize the conversion here so you can use Nullable<DateTime>everywhere otherthan where you're directly using CalendarFromand CalendarTo.

目前还不清楚是什么CalendarFromCalendarTo,但它们可能不支持Nullable<DateTime>自己-但是这将是一个好主意,这里集中了转换,所以你可以使用Nullable<DateTime>随处其他比你在哪里使用直接CalendarFromCalendarTo

回答by shiva

I am not sure what are your CalenderFrom and CalenderTo are; but I am assuming that they are static classess. Based on that assumption, this is how I would write this code.

我不确定你的 CalenderFrom 和 CalenderTo 是什么;但我假设它们是静态类。基于这个假设,这就是我编写这段代码的方式。

public static class CalendarFrom
{
    public static DateTime SelectedFrom { get; set; }
}

public static class CalendarTo
{
    public static DateTime SelectedDate { get; set; }
}

DateTime? startDate = CalendarFrom.SelectedFrom;
            DateTime? endDate = CalendarTo.SelectedDate;

            if (startDate.HasValue 
                && ( startDate.Value != DateTime.MaxValue || startDate.Value != DateTime.MinValue))
            {
            }

回答by Jonathan Wood

DateTimeis a struct and so it can never be null.

DateTime是一个结构体,所以它永远不能为空。

I can think of two options:

我能想到两个选择:

  1. If you really want to support null, use DateTime?to create a nullable DateTime.

  2. Alternatively, it may be appropriate in your application to simply set a DateTimevalue to a default value that indicates no real value has been set. For this default value, you could use either default(DateTime)or something like DateTime.MinValue.

  1. 如果您真的想支持 null,请使用DateTime?创建一个可为 null 的 DateTime。

  2. 或者,在您的应用程序中简单地将一个DateTime值设置为默认值可能是合适的,该值指示尚未设置实际值。对于这个默认值,可以使用两种default(DateTime)或类似的东西DateTime.MinValue