61

I would declare an empty String variable like this:

    string myString = string.Empty;

Is there an equivalent for a 'DateTime' variable ?

Update :

The problem is I use this 'DateTime' as a parameter for a 'StoredProcedure' in SQL. E.g:

    DateTime? someDate = null;
    myCommand.Parameters.AddWithValue("@SurgeryDate", someDate);

When I run this code an exception is catched telling me the 'StoredProcedure' expected a '@SurgeryDate' parameter. But i provided it. Any idea why?

12 Answers 12

92

Since DateTime is a value type you cannot assign null to it, but exactly for these cases (absence of a value) Nullable<T> was introduced - use a nullable DateTime instead:

DateTime? myTime = null;
5
  • Will this 'Nullable' DateTime fit in a SQL Database column of type DateTime ? I'm new to this.
    – phadaphunk
    Apr 2, 2012 at 19:50
  • 1
    Yes - if the column allows inserting null values Apr 2, 2012 at 19:50
  • @PhaDaPhunk Most databases will have a setting for whether or not the value is can be set to null.
    – Servy
    Apr 2, 2012 at 19:51
  • As I said before, you should check your database schema. Whether a column can be null or not is a setting; you need to check if it's set or not and set it if it's not.
    – Servy
    Apr 2, 2012 at 20:08
  • @PhaDaPhunk: You'd have to check for null and insert DBNull.Value in this case, otherwise myTime.Value Apr 2, 2012 at 20:45
42

No. You have 2 options:

DateTime date = DateTime.MinValue;

This works when you need to do something every X amount of time (since you will always be over MinValue) but can actually cause subtle errors (such as using some operators w/o first checking if you are not MinValue) if you are not careful.

And you can use Nullable:

DateTime? date = null;

Which is nice and avoids most issues while introducing only 1 or 2.

It really depends on what you are trying to achieve.

16

You can set a DateTime variable to be '1/1/0001 00:00:00' but the variable itself cannot be null. To get this MinTime use:

DateTime variableName = DateTime.MinValue;
6

You may want to use a nullable datetime. Datetime? someDate = null;

You may find instances of people using DateTime.Max or DateTime.Min in such instances, but I highly doubt you want to do that. It leads to bugs with edge cases, code that's harder to read, etc.

6

Either:

DateTime dt = new DateTime();

or

DateTime dt = default(DateTime);
6

If you set the date to

DateTime someDate = new DateTime();

The value of 'someDate' is set to {1/1/0001 12:00:00 AM}

5

The method you used (AddWithValue) doesn't convert null values to database nulls. You should use DBNull.Value instead:

myCommand.Parameters.AddWithValue(
    "@SurgeryDate", 
    someDate == null ? DBNull.Value : (object)someDate
);

This will pass the someDate value if it is not null, or DBNull.Value otherwise. In this case correct value will be passed to the database.

3
  • ok I like this approach but could you please explain the last line..?
    – phadaphunk
    Apr 2, 2012 at 20:53
  • @PhaDaPhunk: this is for more general scenario - your have a variable someDate of type Nullable<DateTime>. If its value is null I pass DBNull.Value to the AddWithValue method. Otherwise I pass the value itself.
    – Oleks
    Apr 2, 2012 at 21:13
  • Thanks I didn't know we could do that!
    – phadaphunk
    Apr 2, 2012 at 21:15
3

Option 1: Use a nullable DateTime?

Option 2: Use DateTime.MinValue

Personally, I'd prefer option 1.

3

A string is a sequence of characters. So it makes sense to have an empty string, which is just an empty sequence of characters.

But DateTime is just a single value, so it's doesn't make sense to talk about an “empty” DateTime.

If you want to represent the concept of “no value”, that's represented as null in .Net. And if you want to use that with value types, you need to explicitly make them nullable. That means either using Nullable<DateTime>, or the equivalent DateTime?.

DateTime (just like all value types) also has a default value, that's assigned to uninitialized fields and you can also get it by new DateTime() or default(DateTime). But you probably don't want to use it, since it represents valid date: 1.1.0001 0:00:00.

2

There's no such thing as an empty date per se, do you mean something like:

DateTime? myDateTime = null;
1

The .addwithvalue needs dbnull. You could do something like this:

DateTime? someDate = null;
//...
if (someDate == null)
    myCommand.Parameters.AddWithValue("@SurgeryDate", DBnull.value);

or use a method extension...

  public static class Extensions
    {
        public static SqlParameter AddWithNullValue(this SqlParameterCollection collection, string parameterName, object value)
        {
            if (value == null)
                return collection.AddWithValue(parameterName, DBNull.Value);
            else
                return collection.AddWithValue(parameterName, value);
        }
    }
0

This will work for null able dateTime parameter

. .

SearchUsingDate(DateTime? StartDate, DateTime? EndDate){
     DateTime LastDate;
     if (EndDate != null)
       {
          LastDate = (DateTime)EndDate;
          LastDate = LastDate.AddDays(1);
          EndDate = LastDate;
        }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.