none
How do you check for if datetime is null?

    Question

  • I want to check if datetime is null. 
    If it is null assign empty value else assign whatever the value is?

    private DateTime? _modifiedDate;

    public DateTime? ModifiedDate
    {
         get{ return _modifiedDate;}
         set{....................................;}
    }

    creativity..
    Thursday, July 16, 2009 8:22 PM

Answers

  • I think David is try to say that C# is a strongly typed language.

    If you are used to something like Perl or PHP, then setting it to '' would make absolute sense.

    Think of "null" as C#'s empty string.

    DateTime is a struct like int, decimal, double, etc. which is a value type that is not able to be null.

    Adding the ? is making the DateTime a Nullable<DateTime>, which makes it a reference to a value instead of just a value.

    A reference to a value can be null, a value cannot.  Think of a reference as almost like a safe pointer.

    Does this help any?
    • Proposed as answer by wheaties Thursday, July 16, 2009 9:47 PM
    • Marked as answer by Figo Fei Monday, July 20, 2009 9:58 AM
    Thursday, July 16, 2009 9:29 PM

All replies

  • In this situation, it's deceptively easy....

    set { _modifiedDate = value; }

    I'm not kidding.  The value could be either DateTime or DateTime?  If it's DateTime? and it's null, it'll set _modifiedDate to null. Otherwise, it'll set it to the proper value.

    By the way, if that's all you're going to do in the getter and the setter, you can use an Automatic Property if you're in VS 2008 or above:

    public DateTime? ModifiedDate { get; set; }
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Thursday, July 16, 2009 8:23 PM
  • sorry about the wrong question, it was typo.

    i want smthing like this. 
    if the date is null, assign value = ' ' (empty value), if it is not null the get the value.


    creativity..
    Thursday, July 16, 2009 8:27 PM
  • Impossible. 

    The DateTime? doesn't accept ' '.  ' ' is a char, while DateTime? is a DateTime?.  What's the actual problem here?
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Thursday, July 16, 2009 8:29 PM
  • I think David is try to say that C# is a strongly typed language.

    If you are used to something like Perl or PHP, then setting it to '' would make absolute sense.

    Think of "null" as C#'s empty string.

    DateTime is a struct like int, decimal, double, etc. which is a value type that is not able to be null.

    Adding the ? is making the DateTime a Nullable<DateTime>, which makes it a reference to a value instead of just a value.

    A reference to a value can be null, a value cannot.  Think of a reference as almost like a safe pointer.

    Does this help any?
    • Proposed as answer by wheaties Thursday, July 16, 2009 9:47 PM
    • Marked as answer by Figo Fei Monday, July 20, 2009 9:58 AM
    Thursday, July 16, 2009 9:29 PM
  • I think David is try to say that C# is a strongly typed language.

    If you are used to something like Perl or PHP, then setting it to '' would make absolute sense.

    Think of "null" as C#'s empty string.

    DateTime is a struct like int, decimal, double, etc. which is a value type that is not able to be null.

    Adding the ? is making the DateTime a Nullable<DateTime>, which makes it a reference to a value instead of just a value.

    A reference to a value can be null, a value cannot.  Think of a reference as almost like a safe pointer.

    Does this help any?
    Well, yes and no.  What I'm actually getting at is "What is the problem here?"  What's the issue you're trying to solve.  It's understandable to want a simple "" instead of a null, especially in some situations where you're dealing with the UI.  (For example, Javascript setting a null to a textbox would have a strong penchant to put "null" in the textbox.  If that's the case, you'd do better setting it to a null in your object, but having the UI specifically check or this, and adjust for this situation.  Having the business object return an empty string is not a role for the business object to have, more than likely.  It's a role for the UI.  
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Thursday, July 16, 2009 9:54 PM
  • Impossible. 

    The DateTime? doesn't accept ' '.  ' ' is a char, while DateTime? is a DateTime?.  What's the actual problem here?
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    I never said what he was trying to do was sound programming.

    I just felt that he was confused, and some explanation of C# would answer his question better than the above.
    Friday, July 17, 2009 1:08 PM
  • Let me wade in here with my size 10s.

    DateTime also has a readonly property, MinValue, which represents the smallest possible DateTime value. This can be used as a default value instead of null, which is not available because DateTime is a value type, e.g.,

    private DateTime _modifiedDate = DateTime.MinValue;
    
    public bool IsModified()
    {
      return _modifiedDate != DateTime.MinValue;
    }
    
    • Proposed as answer by newdave111 Sunday, May 20, 2012 3:15 AM
    Friday, July 17, 2009 1:25 PM
  • You can also use:

    default(DateTime)

    This is what happens if you declare a DateTime variable and don't set it.

    Which is what I would use, the only issue would be if the user typed in 01-01-0000 00:00:00 in a textbox, you could get a matching value.
    • Proposed as answer by Sunny _Kumar Wednesday, September 26, 2012 6:09 AM
    Friday, July 17, 2009 1:40 PM
  • Excellent...easy thanks. 

    Sunday, May 20, 2012 3:14 AM
  • Thanks, that's a shot in bulls eye... worked like a charm :)
    Wednesday, September 26, 2012 6:10 AM
  • You can solve this problem in two ways, either make it a nullable type, or use the System.DateTime.MinValue.

    More about...C# Datetime null

    Sunday, May 18, 2014 11:16 AM