RudimentalGhost
RudimentalGhost

Reputation: 75

Javascript/Jquery How to format each item in array

Im importing an txt file into an array using jquery like so:

var testarray= new Array();
    $.get('locationtosavedfile', function(data){
            testarray= new Date(data.split('\n'));
            console.log(testarray);
        });

The contents of the file look like so:

"October 12, 2013 06:06:00"
"October 12, 2013 06:36:00"
"October 12, 2013 07:19:00"
"October 12, 2013 07:24:00"
"October 12, 2013 07:39:00"
"October 12, 2013 07:54:00"
"October 12, 2013 08:06:00"
"October 12, 2013 08:46:00"
"October 12, 2013 09:06:00"

The file is loaded into the array fine though its not making a date due to an formatting issue but im sure it should be okay? the text its importing is the same format as

new Date("October 12, 2013 10:12:00");

Which when added manually does work.

The error im getting is: Invalid Date. If I output the array the results are all there and separated by a comma.If there is something i'm missing let me know..

Upvotes: 0

Views: 1187

Answers (1)

Alnitak
Alnitak

Reputation: 339786

You're trying to pass an array of such strings to new Date, not doing them one at a time.

Use ES5 .map to convert an array from one format to another:

testarray = data.split('\n').map(function(v) {
    return new Date(v);
});

Upvotes: 3

Related Questions