How to convert milliseconds to date

Asked

Viewed 4,665 times

3

I would like to know how I can make 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;
        });
    });
},

example of json

[

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

]

Return me this json, I need to convert a field within jobList before return. How can I specifically treat only one field before return?

  • 1

    What is the field that has the date? which is the key (key)? you can put an excerpt of JSON in the question?

  • OK. I made the edit. the time in milisec is in the comment.

  • Great, by the way, there is only one object per jobList, or this jobList is even an array with multiple objects?

  • is an array msm :) in the deployment will have several.

2 answers

3

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

tempo = //milisegundos
date = new Date(tempo);
alert(date.toString());
  • but how do I get the time of the exact json column? I don’t want to convert the entire json. The json is immense.

  • 4

    edit and ask your question an instance of jobList

  • Okay I made the edit. if you can help me I appreciate :)

1


Within that .map() have to iterate all array objects and change/overwrite each date. To traverse the objects you can use a cycle for. Then you have to go to the right property and write it over

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;
  • 1

    Thank you! It was very helpful! :)

Browser other questions tagged

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