3

Sometimes, in a Razor view, I am doing something similar to the following:

@Model.SomeDate1.ToString("MM/dd/yyyy")

This works sometimes, and sometimes it gives me an error about no overload for ToString takes 1 arguments. The answers I've found are similar to what's found here: 'No overload for method 'ToString' takes '1' arguments' error occur While Formatting Datetime Field To String ON "dd-MM-yyyy" format

I know when this error occurs, I can always modify my statement to

@Model.SomeDate2.Value.ToString("MM/dd/yyyy")

and it will work. My question is, why does this happen under these circumstances? These properties are brought through with the same type (non nullable DateTime) at the same time, on the same object, populated from the same table in the same database. Just one requires .Value before the .ToString() and the other doesn't. Why is this?

Community
  • 1
  • 1
Bardicer
  • 1,405
  • 4
  • 26
  • 43
  • 2
    What is the type of `SomeDate1`? Isn't it `DateTime?`, is it? –  Oct 01 '14 at 19:14
  • 4
    For **sure one of the property** is a `Nullable` (`DateTime?`). Only then you will get the error. – Habib Oct 01 '14 at 19:14
  • 3
    `DateTime` does not have a `Value` property. Your property is `DateTime?`. – Blorgbeard Oct 01 '14 at 19:14
  • Are you sure they're both the same type? `.Value` is only available for nullable `DateTime?` – mason Oct 01 '14 at 19:17
  • 2
    Habib (edit: and everyone else), you are correct. The one that is requiring the .Value before the .ToString() is, in fact, a nullable DateTime. The non nullable DateTime has access to ToString without having to use .Value first. Apparently I am just selectively blind to the question marks. Habib, if you will post that as an answer I'll gladly accept it. – Bardicer Oct 01 '14 at 19:18

2 Answers2

4

For sure one of the property is a Nullable (DateTime?). Only then you will get the error.

Nullable<DateTime> or DateTime? doesn't have ToString overload which takes a format parameter. You should check if your Nullable<DateTime> has some value like:

if(SomeDate2.HasValue)
{
  return SomeDate2.Value.ToString("MM/dd/yyyy");
}

If you are not sure about your property, whether it is Nullable<T> or not then you can see this post to determine that.

Also consider using CultureInfo.InvariantCulture when using ToString, because otherwise you might end up with a different separator than / in your output string.

  return SomeDate2.Value.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Habib, even if you check with HasValue on nullable that will still throw an exception if you try to use ToString("MM/dd/yyyy") purely because you need Value if there is no Value ToString() does not accept formatting :-) – Alexander Marinov Oct 01 '14 at 20:09
  • @AlexanderMarinov, thanks, I missed the `Value` in the code, in fact it will not throw an exception, instead there will be a compile time error – Habib Oct 01 '14 at 20:10
3

Well your SomeDate2 is nullable and if you do ToString() on nullable is not allowed with format string. SomeDate1 is a normal DateTime so it works and for SomeDate2 you need to use SomeDate2.Value. But do not forget to check HasValue :-)