1

I have array I'm getting from calling a .net controller.

I'm getting these values for dates:

/Date(1445256000000)/ and /Date(1445256900000)/

Instead of that I want to be able to get proper date values.

Now since I have array of objects I want to be able to update them in the array before sending them to the view.

This is what I have:

$http({
            method: 'GET',
            url: '/Driver/GetDriverTrips',
            params: { id: id }
        }).
        success(function (data) {
            var startDate= new Date(data[0].StartTime.match(/\d+/)[0] * 1);
            alert(myDate);

        });

So this conversion works properly but the thing is that I want to loop through all the same objects in the array, do the conversion and then again save it in the array.

Is there any function I can do that?

3
  • like... ? a for loop? Commented Oct 29, 2015 at 22:01
  • Yes some kind of loop, thx Commented Oct 29, 2015 at 22:02
  • can you post a sample of the returned data? Commented Oct 29, 2015 at 22:03

3 Answers 3

1

try using map

var array = $(data).map(function(){
    return new Date(this.StartTime.match(/\d+/)[0] * 1);;
});

Or, to overwrite StartTime

$.each(data, function(index, element) {
    element.StartTime = new Date(element.StartTime.match(/\d+/)[0] * 1);
});
Sign up to request clarification or add additional context in comments.

4 Comments

how can I add that to the existing array? I need to use the array as a model for my view
thanks, which regex I can use to get MM/DD/YYYY HH:MM ? Thanks
from what? the starttime string? need more info.
Update the question with a example string.
0

My method uses a usual for statement, which is a little more verbose, but I prefer, as long is the most efficient way to traverse an array. So, for each iteration, I got data at position i (data[i]), and stored the result in a temporary variable, called finalDate, and then finally push it to finalDates array. That's what you are looking for?

var finalDates = [];
$http({
    method: 'GET',
    url: '/Driver/GetDriverTrips',
    params: { id: id }
}).
success(function (data) {
    var finalDate;

    for(var i=0; i<data.length; i++) {
        finalDate = new Date(data[i].StartTime.match(/\d+/)[i] * 1);
        finalDates.push(finalDate);
    }
});

Comments

0

Something like

$.each(data, function(index, element) {
    var startDate= new Date(element.StartTime.match(/\d+/)[0] * 1);
});

would loop through all of the returned elements and extract the date.

You can have a second array, say finalDates and push the converted dates into that array as you loop.

4 Comments

can I add the converted values to the existing array? @tyler-sebastian thanks
I would need to know the structure of the returned data, but there's nothing stopping you from data[index] = new Date(data[0].StartTime.match(...; etc
your approach works only for the first element in the array, I'm not able to get data after the first element, I guess its because you have data[0]. This is the structure btw i.gyazo.com/d676e5a16ee1df54b2c229b184700094.png
@Laziale fixed, sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.