0

I have a dotnet core API in which i have a model that looks something like this:

class A {
  public DateTime time {get; set;}
}

I would like to keep the property time as a DateTime, but when sending the response I want to format it as a string with the following format: yyyy-MM-dd.

My problem is that I need to return in either XML or JSON, depending og the header in my request.

So my question is; Without changing the type, how do I make the API return the property time in the format i want for both XML and JSON?

kodeaben
  • 1,829
  • 3
  • 24
  • 33
  • Perhaps you could give this a try: https://stackoverflow.com/questions/5963423/xmlserializer-property-converter – Michael Würthner Jul 07 '20 at 07:13
  • 1
    The **clean** way is to define a DTO which will satisfy all the needs for the response. See https://github.com/jasontaylordev/NorthwindTraders/blob/master/Src/Application/Categories/Queries/GetCategoriesList/CategoryDto.cs – Sir Rufo Jul 07 '20 at 07:26
  • See this https://stackoverflow.com/questions/18635599/specifying-a-custom-datetime-format-when-serializing-with-json-net – Imam San Jul 07 '20 at 07:47
  • Thank you for your suggestions. The DTO was also my 2nd choice, I just hope there was something simpler i could do – kodeaben Jul 08 '20 at 12:18

0 Answers0