Incorrect date on return of json

Asked

Viewed 444 times

1

Good afternoon,

Json’s return brings the badly formatted date = "/Date(1420077600000)/", how to treat ?

{"TB1":1,"DTADMISSAO":"\/Date(1420077600000)\/"}

inserir a descrição da imagem aqui

This is the section where the Data field:

$.ajax({
                        url: "ObterFuncionario",
                        type: "post",
                        datatype: "json",
                        contentType: "application/json charset=utf-8",
                        data: JSON.stringify({ "id": id }),
                        processData: false,
                        beforeSend: function () {
                            $("#divCarregando").show();
                        },
                        complete: function () {
                            $("#divCarregando").hide();
                        },
                        success: function (data) {
                            $("#DTADMISSAO").val(data.DTADMISSAO);    
                        },
                        error: function (result) {
                            alert(result.responseText);
                        }
                    });

HTML:

<div class="form-group small">
                        <div class="col-sm-2">
                            ADMISSÃO:
                        </div>
                        <div class="col-sm-10">
                            <input type="text" id="DTADMISSAO" class="form-control input-sm" />
                        </div>
                    </div>
  • 1

    How you’re populating the value in the field?

  • I added in the question the code section that populates the field.

  • Post the MVC controller code that you are passing the date.

  • the field Voce is returning pro json is a Datetime?

4 answers

1

On the return of your date you can convert it to ToShortDateString, ai in your ajax’s Success it already comes formatted

Example:

DateTime.Now.ToShortDateString();

1

This value that is within Date() is the number of milliseconds since midnight on January 1, 1970.

You can convert to Data again by creating a new date with this value:

function retornaData(valor){
    var dataAdmissao = valor;
    var milisegundos = parseInt(dataAdmissao.slice(dataAdmissao.indexOf('(') + 1, dataAdmissao.indexOf(')')));
    var data = new Date(milisegundos);

    return (("0" + (data.getMonth() + 1)).slice(-2) + "/" +  ("0" + (data.getDate())).slice(-2) + "/" + data.getFullYear())
}

Use:

$("#DTADMISSAO").val(retornaData(data.DTADMISSAO));

Example: https://jsfiddle.net/g5n300ud/1/

References: Date.prototype.getTime(), getTime (Date) (Javascript)

  • Hi Laerte gives error: "Invalid Date"

  • I updated the answer.

1


It has this "contour solution".

var dataAdmissao = obj.DTADMISSAO
var milisegundos = parseInt(dataAdmissao.slice(dataAdmissao.indexOf('(') + 1, dataAdmissao.indexOf(')')));
var data = new Date(milisegundos);

Surely there’s some much better way to do it.

If you can’t find a solution, maybe it’ll do.

  • Filipe worked + or -, how do I format ? is returning like this: Thu Jan 01 2015 00:00:00 GMT-0200 (Brazil’s official time)

  • @Adrianocorder, if you are using the DatePicker, you can use it that way $.datepicker.formatDate('seu-formato-aqui', data);. Otherwise, you can format by hand taking the date parts like this: data.getMonth(), data.getDate(), etc..

-1

Make this change on Controller return

Example:

public ActionResult RetornarData([DataSourceRequest] DataSourceRequest request) {
  DateTime data=DateTime.Now;
  return Json(data.ToString(), JsonRequestBehavior.AllowGet);
}

Browser other questions tagged

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