Change date format on C# / Asp.NET server

Asked

Viewed 1,213 times

0

I’m having a very boring problem, I’m picking one up DataDeCadastro of the server on DateTime when the date well it is in the United States version ("dd/MM/yyyy") I use the .ToString() to format the way I want.

I have a form of editing in which the person can change the registration date, but when I try to send to the server it 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 but error when the month is greater than 12.

This is my Controller :

    [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 after he saves he tries to save so: 09/30/2016 I don’t know how to fix this.

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

  • You want to record it as en en at the bank or display as en?

  • 1

    Use ToString to format is a big problem, huh.

  • the date has no format, the presentation is what it has. You must always generate the text you wish or make the application interpret in this way: http://answall.com/a/141232/101, http://answall.com/q/121440/101, http://answall.com/q/136014/101, http://answall.com/q/118450/101, http://answall.com/q/68247/101

  • So the problem is that at the time of saving I check the 'Datetime' and it’s right , example 'Mounth = 9 and' Day = 11' But when you save it from error.

  • Globalization on the web config solved my problem , vlw

1 answer

1


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

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>
  • Web config has solved my problem thanks.

Browser other questions tagged

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