Change datetimerpicker format to yyyy / mm / dd of DateTime type in c #

0

In this code I'm just showing the date dd / mm / yyyy

datetime fecha = dateTimePickerFalta.Value.Date

I want to get it in yyyy / mm / dd but of type dateTime

How can I do it?

example:

When I select a date, the format it shows is

dd/mm/yyyy

I will use it to make an access query that accepts only the format of

yyyy/mm/dd

This is my complete code where I consult the database that asks me for the yyyy / mm / dd format:

private void Reporte()
        {
            string query = @"select 
                                p.cod, 
                                p.tipo, 
                                p.nombre
                            from Persona p where not exists( 
                                select ma.cod 
                                from Marcaciones ma where 
                                ma.cod = p.cod
                                and (fecha =@fecha) 
                                and (grado = @grado) 
                                and (p.paralelo = @paralelo)";

            OleDbCommand command = new OleDbCommand(query, connection);

            command.Parameters.AddWithValue("@grado", cmbTipoPersona.Text);
            command.Parameters.AddWithValue("@paralelo", cmbParalelo.Text);
            command.Parameters.AddWithValue("@fecha", dateTimePickerFalta.Value);

            adapter.SelectCommand = command;

            DataSetReporte.Clear();

            adapter.Fill(DataSetReporte, "DTFaltas");    /////// ERROR: No coinciden los tipos de datos en la expresión de criterios.  
            dataGridView.DataSource = DataSetReporte;
            dataGridView.DataMember = "DTFaltas";
            dataGridView.Refresh();
        }

This way access accepts it and works as I want

select 
                                p.cod, 
                                p.tipo, 
                                p.nombre
                            from Persona p where not exists( 
                                select ma.cod 
                                from Marcaciones ma where 
                                ma.cod = p.cod
                                and (fecha = fecha  = #2017/11/09# ) 
                                and (grado = @grado) 
                                and (p.paralelo = @paralelo)
    
asked by Mariano 09.11.2017 в 22:15
source

1 answer

-1

Reviewing your update, apply the format when you get the data from the dateTimePicker, example:

string nuevoFormato =  dateTimePickerFalta.Value.ToString("yyyy/MM/dd");

In your code:

command.Parameters.AddWithValue("@fecha", dateTimePickerFalta.Value.ToString("yyyy/MM/dd"));
    
answered by 09.11.2017 в 22:33