C # newbie with DateTime variable I want to set to null

I have an output class with a DateTime variable. I want to clear this to zero in the loader class, but the compiler is complaining:

Cannot convert null to 'System.Data.Time' because it is a non-nullable value type.

As I understand it, but what if I change the type to DateTime? by creating a nullable wrapper, I get:

No overload for 'ToString' method takes arguments '1'

I have an output line that reads.

ACCOUNT_ESTABLISHED_DATE.ToString ("yyyy-MM-dd")

So the question is, when I set a DateTime to be nullable, how do I get around the fact that it no longer behaves like a DateTime that has a formatted ToString?

+2


source to share


10 replies


Use the Value property, for example:



DateTime? dt = DateTime.Now; // or whatever
MessageBox.Show(dt.Value.ToString(...));

      

+7


source


try

ACCOUNT_ESTABLISHED_DATE.Value.ToString("yyyy-MM-dd")

      

You need to access the actual value using the Value property for the NULL type.



You have to make sure the "Value" contains something by checking the ACCOUNT_ESTABLISHED_DATE.HasValue property first.

NTN

+2


source


Whenever you transfer something Nullable<>

(that you do with DateTime?

), you need to do obj.Value.ToString()

.

+1


source


you are looking for

DateTime? dt = new DateTime();

      

or

Nullable<DateTime> dt = new DateTime();
ACCOUNT_ESTABLISHED_DATE.Value.ToString("yyyy-MM-dd");

      

+1


source


You should write:

ACCOUNT_ESTABLISHED_DATE.Value.ToString("yyyy-MM-dd")

      

+1


source


.NET doesn't have a method to do this. You need to have a helper method like:

public string Format(DateTime? date, string format)
{
    if (date == null)
        return string.Empty;

    return date.Value.ToString(format);
}

      

Or even better, an extension method for DateTime?

:

public static class DateTimeExtensionMethods
{
    public static string ToString(this DateTime? date, string format)
    {
        if (date == null)
            return string.Empty;

        return date.Value.ToString(format);
    }
}

      

Then, to use your extension method, only use the code you have and make sure the namespace is DateTimeExtensionMethods

imported into your class.

+1


source


You will need to use

dt.HasValue ? dt.Value.ToString("...") : dt.ToString();

      

This is because it Nullable<T>

is the correct type in its own right, the method is ToString()

already well executed as it handles the situation perfectly null

. But in order to navigate to the underlying nonzero object, you must use the property Value

. But then you will need to check null

(or HasValue

) yourself.

0


source


DateTime? date = getSomeDate();
if (date != null) {
   date.Value.ToString("yyyy-MM-dd");
}

      

0


source


Have you looked at how to set DateTime to DataTime.MinValue?

Suggested here http://dotnetperls.com/datetime-null-minvalue

0


source


  string strDate = string.Empty;
  if(ACCOUNT_ESTABLISHED_DATE != null)
  {
  strDate = ACCOUNT_ESTABLISHED_DATE.Value.ToString("yyyy-MM-dd");
 }

  or you can use null collacing operator

  DateTime newDate  = ACCOUNT_ESTABLISHED_DATE ?? new Date();

   newDate.ToString("yyyy-MM-dd");

      

0


source







All Articles