Change format Date of a JSON statement of a Controller

Asked

Viewed 114 times

0

I have a Controller that generates an output in Json format, below indicated.

The date is returning this in Json 2019-07-22T16:00:04.8579075, I want you to return 22/07/2019 16:00

{
    ordemChegadaId: 1,
    data: "2019-07-22T16:00:04.8579075",
    ordem: 1,
    veiculoId: 29,
    getVeiculo: {
                  veiculoId: 29,
                  tag: "0x300833B2DDD9014000000029",
                  empresaId: 25,
                  getEmpresa: {
                                empresaId: 25,
                                nome: "J G DO VALE - TRANSPORTES ME"
                  },
                  motorista: "BENEDITO MARQUES DOS SANTOS",
                  placa: "AJW-5298",
                  tipoId: 2,
                  getTipo: {
                             tipoId: 2,
                             nome: "TRUCK"
                  }
          }
},

Controller

    // GET: api/OrdensChegadas
    [HttpGet]
    public async Task<ActionResult<IEnumerable<OrdemChegada>>> GetOrdensChegadas()
    {
        return await _context.OrdensChegadas.Include(o => o.GetVeiculo).Include(t => t.GetVeiculo.GetTipo).Include(e => e.GetVeiculo.GetEmpresa).OrderBy(o => o.OrdemChegadaId).ToListAsync();
    }

Class

 public class OrdemChegada
    {
        [Key]
        public int OrdemChegadaId { get; set; }

        [Display(Name = "Data")]
        [DataType(DataType.DateTime)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
        public DateTime Data { get; set; }

        [Display(Name = "Ordem")]
        [Required(ErrorMessage = "O campo {0} é obrigatório")]
        public int Ordem { get; set; }

        [Display(Name = "Veículo")]
        [Required(ErrorMessage = "O campo {0} é obrigatório")]       
        public int VeiculoId { get; set; }

        [Display(Name = "Veículo")]
        public virtual Veiculo GetVeiculo { get; set; }

        [NotMapped]
        [JsonIgnore]
        public string Tag { get; set; }
    }
  • 1

    Why? So it will no longer be a date and will be just any string... It would not just be the case to treat the display of this information?

  • 1

    You will probably have to create a custom serializer for this. Leandro asked the right question: what is the need for this? Removing information from the date may be detrimental to the client application. If you are aware of this and want to actually return it in this format I would advise you to create an intermediate class to represent the JSON that will be returned and, in this class, treat this date as a string - since, in this case, it is just what it will be.

  • You are absolutely correct, this JSON output will be used in a Google spreadsheet and the capture script is encoded in Python, I am having difficulties at the time of performing a Short Date, I will ask a new question.

No answers

Browser other questions tagged

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