M.N
M.N

Reputation: 233

Convert a JSON parsed array containing dates to array type date Javascript

I have this array:

var BigWordsDates = JSON.parse('<?php echo addslashes($Array_OfDates_to_json) ?>');

and it shows up like this (FireBug DOM):

BigWordsDates   Object { #Tahrir=[36], #Egypt=[24], #Morsy=[16], more...}   
#AdminCourt ["2012-10-02","2012-10-02","2012-10-09", 2 more...]

I would like to change it to an array of dates with a format like that: 2012-FEB-06. I would appreciate it if someone can tell how to convert that array to a CSV file.

Upvotes: 1

Views: 311

Answers (1)

ilCrosta
ilCrosta

Reputation: 95

1, you have to declare a JSON for months.

var month = {
  '1': 'JAN',
  '2': 'FEB',

  etc.
}

2, parse your JSON.

var output = [];

for(var k in BigWordsDates['#AdminCourt']) {
    var obj = BigWordsDates['#AdminCourt'][k]; // es. '"2012-10-02"'
    var array = obj.split('-'); // == array['2012', '10', '02']

    var new_value = array[0] + '-' + month[array[1]] + '-' + array[2];

    // add the element to new array
    output.push(new_value);
}

Try! Use try-catch instruction for debug the code.

This method is effective only if your JSON not change.

Upvotes: 1

Related Questions