Api receiving date with changed month and day

Asked

Viewed 288 times

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 takes the class:

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

Then send the object Filters filled:

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) not to change the date? Or inform that the date format is dd/mm/yyyy in the api?

OBS: I don’t 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 at once, that has effect on all.

  • Related: Soen.

  • @Aesir, need to include what you have already done/ tried to do. Put the source code, etc. Take a look at [Ask]

  • I’m complementing, I already update.

  • format your date for it to go in the api with its format

  • Probably the API wasn’t you who made it and you just consume it, right? In this case, the API probably uses American format for dates and you can’t change that. My advice is to format the date before sending to the mm/dd/yyyy format.

  • @Brunoh. I don’t want to change the javascript side because I want a global solution.

  • @Perozzo The API is mine, I want a way to configure it to receive the dates in BR format.

  • 1

    @Brunoh. I need a way to set this up, because if the informed day is greater than 12 it gets as 1 in the API, as there is no month 13 for example, not running the conversion on the API side

Show 3 more comments

1 answer

1


When working with dates on client-side, always try using the date format ISO8601, that when sent to webApi will be correctly transformed by jsonSerializer independent 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 for a string in this format, with the .toISOString()

NOTE: I don’t want to use javascript functions that convert the date, because I’d have to do it every time I had a date on the project, I guess wrong, I want to solve at once, that has effect on all.

As for the above comment, I think you should ask yourself how a system will work that deals with various nationalities and date formats. Adopt a standard of communication (such as ISO8601) can bring you less complexity than needing to exchange the date patterns of your system according to who is making the request.

Related Soen:

  • 1

    Studying about your answer and analyzing the options I have to solve this problem, really yours is the best!

  • I’ve had enough trouble with datetime dealing with Web Api, there’s even a nice article about why Cultureinfo doesn’t directly affect the dates received in the controllers. I’ll see if I can find you here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.