How to convert milliseconds to date

3

I would like to know how I can do a millisecond to date conversion within a field in a json.

For example

$http.get("http://teste").success(function (dados) {

        ng.locations = $.map(dados, function(job) {

                return job.jobList;
        });
    });
},

json example

[

{
    "description":"Job de Teste",
    "jobList":[
        {
            "jobID":01,
            "jobState":"OK",
            "jobName":"TESTE",
            "jobCompleteTime":1407783800000 //tempo em milisec.
        }
    ]
}

]

I get this json, I need to convert a field inside the jobList before returning. How can I specifically handle only one field before return?

    
asked by anonymous 14.08.2014 / 20:30

2 answers

1

Within this .map() , they have to iterate all objects in the array and change / overwrite each date. To cycle through the objects you can use a for cycle. Then you have to go to the correct property and overwrite it.

for(var i = 0; i < job.jobList.length; i++){
    var data = job.jobList[i].jobCompleteTime;
    job.jobList[i].jobCompleteTime = new Date(data).toString(); // fica "Mon Aug 11 2014 21:03:20 GMT+0200 (W. Europe Daylight Time)"
}
// aqui fazer o return job.jobList;

If you want to format to another format, for example: yyyy-mm-dd, you can do

for(var i = 0; i < job.jobList.length; i++){
    var data = job.jobList[i].jobCompleteTime;
    var d = new Date(data);
    job.jobList[i].jobCompleteTime = d.getFullYear()+ "-" + (d.getMonth() + 1) + "-" + d.getDate();
}
// aqui fazer o return job.jobList;
    
15.08.2014 / 15:11
3

Just pass the time in milliseconds to the constructor of type Date() :

tempo = //milisegundos
date = new Date(tempo);
alert(date.toString());
    
14.08.2014 / 20:36