How to return a Date value from JSON to Google Visualization API How to return a Date value from JSON to Google Visualization API json json

How to return a Date value from JSON to Google Visualization API


I just ran into this problem myself, so I thought I'd paste the answer from the google api documentation, located here http://code.google.com/apis/chart/interactive/docs/dev/implementing_data_source.html#jsondatatable

"JSON does not support JavaScript Date values (for example, "new Date(2008,1,28,0,31,26)"; the API implementation does. However, the API does now support a custom valid JSON representation of dates as a string in the following format: Date(year, month, day[,hour, minute, second[, millisecond]]) where everything after day is optional, and months are zero-based."


I was running into same problem and above solution did not work. After searching for hours I found following post and the solution in there worked.

https://groups.google.com/forum/#!msg/google-visualization-api/SCDuNjuo7xo/ofAOTVbZg7YJ

Do not include "new" in the json string so it will be just: v:"Date(2009, 9, 28)"


I suppose that the quote is not at the correct place in your snippet "new Date(2010, 3, 28")Write instead "new Date(2010, 3, 28)"

Json format does not accept javascript object so the server return a string. JSON knows only numbers, boolean constants, string, null, vector and 'object' (much more a dictionnary).

I suppose that you have to perform an eval() of the returned string (do not forget to check inputs).

Another alternative is to use a Regex to extract the fields something like /new Date\((\d+),(\d+),(\d+)\)/ will work.