Search code examples
javascriptdatetimegoogle-visualizationtimelinechap-links-library

Google Visualizate Timeline Chart dates are strangely a month off...what am I missing?


I'm using this demo as a template: http://almende.github.io/chap-links-library/js/timeline/examples/example05_format_custom_html.html

It's a great timeline, but even in the official example, the source code shows July dates, like new Date(2010, 7, 19) and the timeline shows it a month later, in August? I'm echoing php dates into the Date(), and I'm having the same problem. I thought it may be because of the array starting at 00, but wouldn't the example at least be correct? Just wondering if there's an easier fix than subtracting 1 from all my months. Thanks for any help!!

S


Solution

  • JavaScript months are 0 indexed.

    As a result, January is 0, February is 1, etc. Thus, when you match it up to your data, everything appears to be a month off.

    new Date("2013-01-05").getUTCMonth(); // 0
    new Date("2013-02-05").getUTCMonth(); // 1
    new Date("2013-12-05").getUTCMonth(); // 11