0

I have a model for my database that has a DateTime property. However, this property is really a Date. The extra time component is causing issues when sorting/filtering on the client. Many tables/client components have this problem.

I was hoping I could fix this at the model level, and only retrieve the Date portion whenever it is accessed with the following modification.

private DateTime invoiceDate;
[Required]
public DateTime InvoiceDate { get => invoiceDate.Date; set =>invoiceDate = value; }

However, this does not seem to be working. Is something like this possible?

Edit: I have discovered C# allows more specific DataType specifications on models - https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

i.e.

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }

However, when I hit my API I am still getting the full database information

  "InvoiceDate": "2019-06-14T00:00:00-07:00"

Why isn't C# fixing this with the extra specification provided?

Edit:

I have also found this helpful thread which suggested adding a JSON converter to the property, but that has not fixed the problem either. The time is still being sent.

James L.
  • 12,893
  • 4
  • 49
  • 60
  • The data type you're using is still a `DateTime` so the time will always be there, even if it's all zeroes. You can't change that. If you want a date-only *data type*, take a look at [NodaTime](https://www.nuget.org/packages/NodaTime). Its `LocalDate` data type is probably what you want. There's a separate package for adding NodaTime support to Json.NET. You may find yourself writing type converters for other situations. – madreflection Jun 20 '19 at 18:12
  • That's a good point, and actually I am fine with the time being present, so long as it is all zeroed out. Just so long as it's consistent, filtering will work. – James L. Jun 20 '19 at 19:03
  • Have you tried formatting the date from the API on your client and do the filter/sorting after? – Luke Villanueva Jun 25 '19 at 08:22
  • I ended up getting this to work thru OData request modification. I had to edit the string to wrap the property in `data(prop)` and then I had to edit the value to remove the time from javascript's DateTime.ToString `yyyy-MM-dd` – James L. Jun 25 '19 at 16:36

2 Answers2

0

You can use Bind with exclude for your property

[Bind(Exclude="InvoiceDate")]
    public class YourModel{

    }
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • But then updates/creates would not set this property right? I still want the current controller actions to work – James L. Jun 20 '19 at 16:29
0

I ended up getting this to work thru OData request modification. I had to edit the string to wrap the property in data(prop) and then I had to edit the value to remove the time from javascript's DateTime.ToString yyyy-MM-dd

James L.
  • 12,893
  • 4
  • 49
  • 60