Change date format on C # / Asp.NET server

0

I am having a very annoying problem, I am getting a DataDeCadastro from the server that is in DateTime when the date well is in the US version ("dd/MM/yyyy") I use .ToString() to format as I want to.

I have a way of editing where the person can change the date of registration, but when I try to send to the server he reverses the day with the month, I do not know how to deal with this situation, I tried to reverse again with the ToString and assign to a DateTime of error when the month is greater than 12.

My Controller is this:

    [HttpPost]
    [ValidateInput(false)]
    public JsonResult Editar(string Titulo, string Descricao, DateTime DataCadastro, int ProdutoId)
    {
        int idProduto = Convert.ToInt32(ProdutoId);
        var produto = ProdutoServico.GetById(idProduto);
        if (Titulo != null && Descricao != null && DataCadastro != null )
        {

            produto.Descricao = Descricao;
            produto.DataCadastro = DataCadastro;
            produto.Titulo = Titulo;
            ProdutoServico.Update(produto);
            if (produto.CategoriaProdutoId == null)
            {
                return Json(new { redirectUrl = Url.Action("Index", "Produto", new {CategoriaId = 0, area = "Lojista" }), isRedirect = true });
            }
            else
            {
                return Json(new { redirectUrl = Url.Action("Index", "Produto", new { CategoriaId = produto.CategoriaProdutoId.Value, area = "Lojista" }), isRedirect = true });
            }
        }
        return Json(new { redirectUrl = Url.Action("Index", "Produto", new { CategoriaId = produto.CategoriaProdutoId.Value, area = "Lojista" }), isRedirect = true });
    }

In this controller the date arrives correct example: 30/09/2016 but it after saving it tries to save it like this: 09/30/2016 do not know how to solve this.

Remembering that the server is in the US, thank you very much.

    
asked by anonymous 11.10.2016 / 15:04

1 answer

1

You can try in several ways: Programmatically force the date culture to be displayed with the following excerpt:

System.Globalization.CultureInfo brasil = new System.Globalization.CultureInfo("pt-BR");
String dataBr = DateTime.Now.ToString(brasil);

Or specify in your Web.config file the culture to use:

<configuration>
  <system.web>
    <globalization culture="pt-BR" uiCulture="pt-BR" />
  </system.web>
</configuration>
    
11.10.2016 / 16:25