How to convert data to string from JSON? (Javascript MVC C#)

Asked

Viewed 144 times

1

The data looks like this:

Date(-4735141200000)

for me:

function (dados) {
    console.log(dados);

    var len = dados.length;
    var txt = "";
    var toDate;
    if (len > 0) {
      for (var i = 0; i < len; i++) {

        if (dados[i].nome && dados[i].datadenascimento && dados[i].cpf && dados[i].departamento) {

            toDate = new Date(dados[i].datadenascimento);
            console.log(toDate);
            txt += "<tr><td>" + dados[i].nome + "</td><td>" + dados[i].datadenascimento + "</td><td>" + dados[i].cpf + "</td><td>" + dados[i].departamento + "</td><td><a href='javascript:editar(" + dados[i].idfuncionario + ")'>Editar</a></td><td><a href='javascript:deletar(" + dados[i].idfuncionario + ")'>Deletar</a></td></tr>";
       }
    }
    if (txt != "") {
        $("#table").append(txt).removeClass("hidden");
    }
}
  • looks like the date is coming in format Timestamp. Have a look at this link http://stackoverflow.com/a/21523011/2588695

  • What is expected from this entry? Something like "17/01/2017"?

  • Maybe this function will help: function ToJavaScriptDate(value) {&#xA;&#xA; var pattern = /Date\(([^)]+)\)/;&#xA; var results = pattern.exec(value);&#xA; var dt = new Date(parseFloat(results[1]));&#xA; return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();&#xA;&#xA;} ToJavaScriptDate("Date(-4735141200000)")

1 answer

0

Thanks for the help, but I managed to fix it. I used the following code:

jsondata = function (i) {

                    var jsonDate = "/Date(1245398693390)/"; //dados[i].datadenascimento;
                    var re = /-?\d+/;
                    var m = re.exec(jsonDate);
                    var d = new Date(parseInt(m[0]))
                    var day = d.getDate();
                    var month = d.getMonth() + 1;
                    var year = d.getFullYear();
                    if (day < 10) {
                        day = "0" + day;
                    }
                    if (month < 10) {
                        month = "0" + month;
                    }
                    var date = day + "/" + month + "/" + year;

                    return date;
                    //tratar data
                }

//entra "/Date(1245398693390)/" e retorna 10/07/1990, por exemplo

Browser other questions tagged

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