How to convert MM/dd/yyyy to dd/MM/yyyy?

Asked

Viewed 235 times

0

I think because my computer is from the USA it always saves the dates in American format, anyway, I have a Date Time Picker that on the client’s computer is in Brazilian format, but when he saves this data C# changes the days for the months.

Ex: If the customer wants to save the date 09/03/2020 the saved date is 03/09/2020

   Classes.VendasServicos vs = new Classes.VendasServicos();
                    Venda venda = new Venda();
                    venda.idCliente = int.Parse(cmbCliente.SelectedValue.ToString());
                    venda.carro = txtCarro.Text;
                    venda.placa = txtPlaca.Text;
                    venda.data = DtVenda.Value;
                    Conexao conexao = new Conexao();
                    conexao.conectar();


 var escolha = MessageBox.Show("O cliente efetuou o pagamento de R$" + preco + ",00 ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (escolha == DialogResult.Yes)
                    {
                        int linhas = conexao.executar($"INSERT INTO Vendas(data, carro, placa, idCliente, pago) VALUES('{venda.data.ToShortDateString()}','{venda.carro}','{venda.placa}','{venda.idCliente}', 1)  ");
                    }
                    else
                    {
                        int linhas = conexao.executar($"INSERT INTO Vendas(data, carro, placa, idCliente, pago) VALUES('{venda.data.ToShortDateString()}','{venda.carro}','{venda.placa}','{venda.idCliente}', 0)  ");
                    }

2 answers

1

Remove the .ToShortDateString() of your insertion query, c# by itself with the object DateTime created in the right format will store the date in the correct format in the database.

0

Oops! I performed the tests on another computer, and what I found is the following no matter how is the Datetimepicker (MM/dd/yyyy or dd/MM/yyyy) it will always reverse and exchange the day for the month, so it does not save the date coreta. Doing these tests I discovered another bug, every time I will save a date earlier or later than today it from conversation error, however neither of these two errors occur when I am testing on my pc.

Browser other questions tagged

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