Api receiving date with month and day changed

0

I have a class:

public class Filtros{
    public DateTime DataInicial { get; set; }
    public DateTime DataFinal { get; set; }
    //outros campos...
}

I have a C # API that receives the class:

[HttpPost("ObterProvisoes")]
public JsonResult ObterProvisoes(Filtros filtros)
{
    //recebe mm/dd/yyyy
}

Then I send the filled Filters object:

var filtros = {
          DataInicial: self.DataInicial, // dd/mm/yyyy
          DataFinal: self.DataFinal // dd/mm/yyyy
}

$.post("/api/RelatorioFinanceiro/ObterProvisoes", filtros, function () {

}).done(function (response) {
        //funcoes
});

When I send the completed object the date is in the format dd / mm / yyyy, but the api receives as mm / dd / yyyy. Is there any way to configure the API or project (asp.net core 1.1) to not change the date? Or inform you that the date format is dd / mm / yyyy in the api?

I do not want to use functions in javascript that convert the date, because I would have to do this every time I had a date in the project, I think it's wrong, I want to solve it all at once.

    
asked by anonymous 12.07.2017 / 14:52

1 answer

1

When working with dates in client-side , always try to use the date format ISO8601 , which when sent for webApi will be correctly transformed by jsonSerializer regardless of the location configured on the client side.

The date format defined in ISO is yyyy-MM-ddTHH-mm-ss.sssz , and javascript itself has a method to transform an object of type Date to string in this format, with .toISOString ()

  

NOTE: I do not want to use functions in javascript that convert the date because   I would have to do this every time I had a date on the project, I think   wrong, I want to resolve at once, to have effect on all.

As for the above comment, I believe you should ask yourself how a system will work with different nationalities and date formats. Adopting a communication pattern (such as ISO8601 ) can bring you less complexity than having to change the date patterns of your system according to who is making the request.

Related SOen:

12.07.2017 / 19:14