0

I am trying to display the date format "dd/MM/yyyy" in an example json in Swagger. However, when I want to show it, it shows me like this:

{
  "contractCode": 0,
  "registers": 0,
  "totalDue1": 0,
  "totalDue2": 0,
  "buildingExpenses": [
    {
      "ownersAssociationCode": 0,
      "functionalUnitCode": 0,
      "period": "hola",
      "ownerName": "string",
      "location": "string",
      "ownerEmail": "string",
      "dueDate1": "2021-12-20T00:00:00",
      "amount1": 0,
      "amount2": 0,
      "electronicPaymentCode": "string",
      "barcode": "string"
    }
  ]
}

I tried to format it with parse and parseExact, but neither worked. I leave my code: public class BuildingExpenseModeExample : IExamplesProvider

{
    public object GetExamples()
    {
        var dueDate1 = DateTime.Now.ToString("dd/MM/yyyy");

        return new BuildingExpenseResumeInputDto
        {
            ContractCode = 0,
            Registers = 0,
            TotalDue1 = 0,
            TotalDue2 = 0,
            BuildingExpenses = new List<BuildingExpenseDetailInputDto>
            {
                new BuildingExpenseDetailInputDto
                {
                    OwnersAssociationCode = 0,
                    FunctionalUnitCode = 0,
                    Period = "hola",
                    OwnerName = "string",
                    Location = "string",
                    OwnerEmail = "string",
                    DueDate1 = DateTime.ParseExact(dueDate1, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    //DueDate1 = DateTime.ParseExact(DateTime.Today.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    Amount1 = 0,
                    Amount2 = 0,
                    ElectronicPaymentCode = "string",
                    Barcode = "string"
                }
            }
        };
    }
}

I hope you can help me!

  • What is being returned? – Peter Smith Dec 20 '21 at 14:27
  • 3
    Why are you taking the current time, converting to a string, then converting it back to a DateTime (ParseExact)? – gunr2171 Dec 20 '21 at 14:29
  • 2
    Looks like `DueDate1` is of DateTime type. Swagger will display the date using its own format. `DateTime.ParseExact` return DateTime value after parsing the input date string. If you want your code to return date with specific format then you need to have `string DueDate1`. – Chetan Dec 20 '21 at 14:29
  • Currently it returns: "2021-12-20T00:00:00" @PeterSmith. – Gianfranco Grigera Dec 20 '21 at 14:30
  • 1
    Does this answer your question? [Formatting DateTime in ASP.NET Core 3.0 using System.Text.Json](https://stackoverflow.com/questions/58102189/formatting-datetime-in-asp-net-core-3-0-using-system-text-json) – gunr2171 Dec 20 '21 at 14:30
  • 2
    DateTime has no format, it's a binary value. If you want to display that DateTime in a certain way do so on the UI – Panagiotis Kanavos Dec 20 '21 at 14:31
  • Because I want today's date to be displayed. But then I give it the format I need. @gunr2171 – Gianfranco Grigera Dec 20 '21 at 14:31
  • 1
    But all that conversion back and forth doesn't mean anything if the DueDate1 property is a DateTime. The json converter in asp.net will transform the object into a json string. See the link above on ways of customizing it. – gunr2171 Dec 20 '21 at 14:33
  • Perfect. I will be watching that. Thanks a lot! – Gianfranco Grigera Dec 20 '21 at 14:34
  • 1
    @GianfrancoGrigera returns where? DateTime has no format, that's not up to debate. The standard date format for JSON is ISO8601, so that value is perfectly valid. What are you trying to do? Where do you want to display the localized string? – Panagiotis Kanavos Dec 20 '21 at 14:35
  • 3
    @GianfrancoGrigera *don't* customize anything. There's a standard way of serializing dates to JSON - ISO8601. Any other format is just text, not a date. If you want any other service to understand your JSON you need to use ISO8601. The JSON string is fine the way it is. The linked question asked how to emit `Z` for UTC, not how to change from ISO8601 – Panagiotis Kanavos Dec 20 '21 at 14:36
  • 1
    Specifically in [RFC7493 - The I-JSON Message Format: Time and Date Handling](https://datatracker.ietf.org/doc/html/rfc7493#section-4.3): `It is RECOMMENDED that all such data items be expressed as string values in ISO 8601 format` – Panagiotis Kanavos Dec 20 '21 at 14:42

1 Answers1

0

Finally, I was able to correct it by adding these two lines of code to my Startup:

services.AddControllers().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
                options.SerializerSettings.DateFormatString = "dd/MM/yyyy";
            });

With that it was solved since within my api there was already a DateTimeConverter. Thank you very much to all!