-1

I am trying to change the format of a nullable datetime field in a table, so that it displays in the format ddd dd mmm yyyy. I have tried using DateTime.ToString("ddd dd mmm yyyy), and DateTime.Value.ToString("ddd dd mmm yyyy"), but neither have worked.

<td>
  @Html.DisplayFor(modelItem => item.DailyReportDate)
</td>

Any advice would be appreciated.

Pragmateek
  • 13,174
  • 9
  • 74
  • 108
Kevin
  • 198
  • 3
  • 6
  • 17
  • 1
    "but neither have worked" doesn't help. What is the expected output, what is the actual output? – ken2k Jun 30 '14 at 11:50
  • Is it not meant to be ("dddd, dd MMMM yyyy")? and NOT ("ddd dd mmm yyyy") – BossRoss Jun 30 '14 at 11:50
  • "neither have worked" - please give the *exact* problems. Wrong values? No values? Compile-time failure? Note that `m` means minute, not month. Perhaps you just wanted `MMM`? If you'd explained the symptoms in your questions, we'd know... – Jon Skeet Jun 30 '14 at 11:50
  • Why don't you just do `@item.ToString("some format string")`? – Dethariel Jun 30 '14 at 11:51
  • WHen I use the ToSTring method it gives the error "Method ToSTring has 0 parameters but is invoked with 1 argument". I think this is because it is a nullable DateTime, as opposed to a DateTime, but I don't know how to get round this – Kevin Jun 30 '14 at 11:53
  • This should work -> item.Value.ToString("ddd dd mmm yyyy) (checked). Are you sure that type is "DateTime?"? – Artyom Kharlamov Jun 30 '14 at 11:57
  • Yeah it's definietly DateTime: public Nullable DailyReportDate { get; set; } – Kevin Jun 30 '14 at 12:00
  • When I try to use item.Value it gives the error: Cannot resolve symbol 'Value' – Kevin Jun 30 '14 at 12:01
  • That's because you need to use: item.DailyReportDate.Value.ToString("ddd dd mmm yyyy) – Artyom Kharlamov Jun 30 '14 at 12:03
  • The code builds now using that, but crashes at that line, throwing the error: An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. – Kevin Jun 30 '14 at 12:12
  • You're using it wrong. You typed `@Html.DisplayFor(modelItem => item.DailyReportDate.Value.ToString("format"))`, but should have instead typed `item.DailyReportDate.Value.ToString("format"))` (without `DisplayFor`). See [this question](http://stackoverflow.com/questions/19417311/formatting-datetime-error-templates-can-be-used-only-with-field-access-propert) on why does this happen – Dethariel Jun 30 '14 at 12:18

2 Answers2

11

The problem here is that the DateTime? has no ToString() method which accepts formatting string. To overcome this, you can use the following piece of code:

@string.Format("{0:ddd dd mmm yyyy}", item.DailyReportDate)

If DailyReportDate is null, then this code will render as an empty string. Otherwise, you get your formatted datetime. This behavior is described in Nullable.ToString Method documentation

Hope that helps.


Not exactly what you want, but this can help you.

If you had a simple DateTime (non-nullable), then here is a good example on how to accomplish what you want. To summarize, all you need to do is to set a DisplayFormatAttribute attribute on your property

public class Model {
    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    public DateTime? DailyReportDate { get; set;}
}

and then you can use it like pointed out:

@Html.DisplayFor(item=> item.DailyReportDate)

You can find the documentation for DisplayFormatAttribute here

Community
  • 1
  • 1
Dethariel
  • 3,394
  • 4
  • 33
  • 47
0

.ToString() always come up with "object reference not set to an instance of an object" as this method required the not null object. You must use the if else like this

@if (modelItem => item.DailyReportDate != null)
{
    //add format
}
else
 {
   @Html.DisplayFor(modelItem => item.DailyReportDate)item.Amount)
 }
}
fakhre
  • 11
  • 2