1

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.

6
  • 4
    What have you tried? Commented Nov 24, 2012 at 10:36
  • 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. Commented Nov 24, 2012 at 11:45
  • also i googled and found this too: mattkruse.com/javascript/date but couldnt apply it to my code. Commented Nov 24, 2012 at 11:47
  • do i have to download Date.js to use it ? or this problem can be solved with another method ?! Commented Nov 24, 2012 at 12:03
  • 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? Commented Nov 24, 2012 at 13:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.