Incorrect date on return of json

1

Good afternoon,

The return of Json brings the badly formatted date="/ Date (1420077600000) /", how to deal?

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

ThisisthesectionwhereIsettheDatefield:

$.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>
    
asked by anonymous 22.10.2015 / 18:46

3 answers

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 is a better way to do this.

If you can not find a solution, maybe it will work.

    
22.10.2015 / 19:22
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())
}

Usage:

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

Example: link

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

    
22.10.2015 / 19:13
1

On your return date you can convert it to ToShortDateString , then in the success of your ajax it has already been formatted

Example:

DateTime.Now.ToShortDateString();
    
22.10.2015 / 20:45