locked
DateTime Default Value RRS feed

  • Question

  • Hello,

    I have a class with a property of type DateTime.

    What is the default value for the DateTime type?

    Can I check if a value was assigned to it without making it nullable?

    Thanks,

    Miguel

    Monday, August 30, 2010 8:43 PM

Answers

  • To add to Suha's answer, you cannot know if a DateTime field has been assigned DateTime.MinValue or simply holds the default value.  Programmatically you can assume the field is not assigned.  Usually DateTime.MinValue is not a valid date to enter in a user interface, in which case you can assume if the value is MinValue the user has not yet assigned a value.  MinValue is often used to indicate unassigned dates.  However, a nullable DateTime might be a better option.
    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    • Marked as answer by Bin-ze Zhao Thursday, September 2, 2010 7:39 AM
    Tuesday, August 31, 2010 5:52 AM

All replies

  • the default value of the DateTime is DateTime.MinValue  (01/01/0001)

     

    DateTime date; 

    //validate if the parameter has an assigned value or not

    if (date != DateTime.MinValue)

    {

    }

    Monday, August 30, 2010 8:52 PM
  • To add to Suha's answer, you cannot know if a DateTime field has been assigned DateTime.MinValue or simply holds the default value.  Programmatically you can assume the field is not assigned.  Usually DateTime.MinValue is not a valid date to enter in a user interface, in which case you can assume if the value is MinValue the user has not yet assigned a value.  MinValue is often used to indicate unassigned dates.  However, a nullable DateTime might be a better option.
    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    • Marked as answer by Bin-ze Zhao Thursday, September 2, 2010 7:39 AM
    Tuesday, August 31, 2010 5:52 AM