开发者

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

开发者 https://www.devze.com 2022-12-08 01:31 出处:网络
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:

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 D开发者_如何学JAVAateTime that has the formatted ToString available?


Use its Value property, like so:

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


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


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


You should write:

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


.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.


are you looking for

DateTime? dt = new DateTime();

or

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


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.


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

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


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


  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

精彩评论

暂无评论...
验证码 换一张
取 消