I do not know how to make my api return a string like datetime

1

I have to return a datetime from a string in my asp.net project to feed a calendar that only accepts datetime. The string is a two-digit number that represents the expiration date of a product. I convert it with the following code to DateTime, but it shows an error that I understand, but I do not know how to solve it.

"Can not imply convert system.datetime to string"

I can not apply dt2.ToString (); Because the calendar plugin would not read it since it only accepts DateTime. Thank you!!

public IHttpActionResult GetPolizas()
    {
        var polizaDto = _context.Poliza
            .Include(m => m.UsoVehiculo)
            .Include(m => m.Compania)
            .Include(m => m.MedioPago)
            .Include(m => m.Cliente)
            .ToList();


        foreach (var poliza in polizaDto)
        {
            var fecha = poliza.Vencimiento;
            fecha = DateTime.Now.Month + "-" + poliza.Vencimiento + "-" + DateTime.Now.Year + " "+ "00:00:00";

            DateTime dt2 = DateTime.ParseExact(fecha,
                    "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
            poliza.Vencimiento = dt2;

        };



        return Ok(polizaDto);
    }
    
asked by Santiago Grossi 26.12.2018 в 01:28
source

1 answer

1

If the property% co_of% is a Datetime maybe what you want to do is something like this

foreach (var poliza in polizaDto)
{
    string fecha = string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Month, poliza.Vencimiento.Day, DateTime.Now.Year);

    DateTime dt2 = DateTime.ParseExact(fecha, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);

    poliza.Vencimiento = dt2;

};

As you will see the Vencimiento is used to take only the day

    
answered by 26.12.2018 в 15:04