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.

share|improve this question
4  
What have you tried? – H2CO3 2 days ago
i tried using the DateObject and some how convert it but gave me NAN.it was Date.Parse i think and i sent it the whole array. – M.N yesterday
also i googled and found this too: mattkruse.com/javascript/date but couldnt apply it to my code. – M.N yesterday
do i have to download Date.js to use it ? or this problem can be solved with another method ?! – M.N yesterday
So do you want to convert the date strings into date objects, or into a different format? Where does a CSV file come into it? – Hemlock yesterday
show 1 more comment
feedback

1 Answer

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.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.