Subtract 3 days from sql dates with c # [duplicate]

-1
    public List<DateTime> fechas3()
    {
        List<DateTime> list = new List<DateTime>();
        List<DateTime> fec = new List<DateTime>();
        SqlConnection cone = new SqlConnection(connect);
        cone.Open();
        SqlCommand cmd = new SqlCommand("SELECT DispositionDueDate FROM fechas", cone);
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            fec.Add(Convert.ToDateTime(reader[0]));
        }
        return fec;
    }

The previous code prints me dates but I would like to subtract three days from each of those dates and show them on the screen with those three days less.     Ex:

  antes               despues 
 20/5/16   ======>>   17/5/16
 15/12/16  ======>>    12/12/16
    
asked by use2105 07.12.2016 в 20:32
source

3 answers

2

You could subtract the direct date in the sql

string query = "SELECT DATEADD(day,-3, DispositionDueDate) as DispositionDueDate FROM fechas";

SqlCommand cmd = new SqlCommand(query, cone);

as you will see with the DATEADD () in the same query you can subtract days

    
answered by 07.12.2016 в 20:41
1

You can

while (reader.Read())
{
   DateTime fecha = Convert.ToDateTime(reader[0]);
   fecha = fecha.AddDays(-3);
   fec.Add(fecha);
}
    
answered by 07.12.2016 в 20:34
0

Test using the AddDays method of DateTime. In your example:

fec.Add(Convert.ToDateTime(reader[0]).AddDays(-3));
    
answered by 07.12.2016 в 20:36