John Putnam
John Putnam

Reputation: 123

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

I have an output data class with a DateTime variable. I want to clear that to a null value in a loader class but the compiler complains with:

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

I understand that, but if I change the type to DateTime? creating the nullable type wrapper I get:

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

I have an output line that reads.

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

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

Upvotes: 2

Views: 11273

Answers (10)

anishMarokey
anishMarokey

Reputation: 11397

are you looking for

DateTime? dt = new DateTime();

or

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

Upvotes: 1

sudhansu63
sudhansu63

Reputation: 6180

  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");

Upvotes: 0

tofi9
tofi9

Reputation: 5853

.NET doesn't have a method out of the box for this. You'd 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, just use the code you have in your question and make sure the namespace of the DateTimeExtensionMethods is imported into your class.

Upvotes: 1

Fernando
Fernando

Reputation: 4028

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

Upvotes: 0

Brandon Haugen
Brandon Haugen

Reputation: 959

Have you looked at setting the DateTime to DataTime.MinValue?

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

Upvotes: 0

empi
empi

Reputation: 15881

You should write:

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

Upvotes: 1

MusiGenesis
MusiGenesis

Reputation: 75276

Use its Value property, like so:

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

Upvotes: 8

user61470
user61470

Reputation:

try

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

You need to access the actual value using the 'Value' property of the nullable type.

You should make sure 'Value' contains something first testing the ACCOUNT_ESTABLISHED_DATE.HasValue property.

HTH

Upvotes: 2

Agent_9191
Agent_9191

Reputation: 7253

Whenever you wrap something Nullable<> (which is what you're doing with DateTime?), you need to do obj.Value.ToString().

Upvotes: 1

Joey
Joey

Reputation: 354536

You would have to use

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

This is because Nullable<T> is a proper type in its own right whose ToString() method is already nicely done, as it handles the null case well. But to get to the underlying non-nullable object you have to use the Value property. But then you'll have to check for null (or HasValue) yourself.

Upvotes: 0

Related Questions