Javascript Google图表API:显示的日期不正确

Javascript Google图表API:显示的日期不正确,javascript,google-visualization,Javascript,Google Visualization,我有一个奇怪的问题,我很困惑,但我相信这里会有人知道我做错了什么。我的所有日期显示不正确(即六月显示七月,七月显示八月) 我的代码在这里: // Load the Visualization API and the piechart package. google.load('visualization', '1.0', {'packages':['corechart']}); // Set a callback to run when the Google Visualization API

我有一个奇怪的问题,我很困惑,但我相信这里会有人知道我做错了什么。我的所有日期显示不正确(即六月显示七月,七月显示八月)

我的代码在这里:

// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawVisualization);

// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawVisualization() {

    var chartTable = new google.visualization.DataTable();
    chartTable.addColumn('date', 'Date');
    chartTable.addColumn('number', 'Sell');
    chartTable.addColumn('number', 'GP');
    chartTable.addRows(6);

    chartTable.setValue(0, 0, new Date( 2011, 06, 22 ));
    chartTable.setValue(0, 1, 1316.90);
    chartTable.setValue(0, 2, 456.05);
    chartTable.setValue(1, 0, new Date( 2011, 06, 21 ));
    chartTable.setValue(1, 1, 1793.70);
    chartTable.setValue(1, 2, 531.10);
    chartTable.setValue(2, 0, new Date( 2011, 06, 20 ));
    chartTable.setValue(2, 1, 13559.25);
    chartTable.setValue(2, 2, 1337.75);
    chartTable.setValue(3, 0, new Date( 2011, 06, 17 ));
    chartTable.setValue(3, 1, 3034.15);
    chartTable.setValue(3, 2, 892.30);
    chartTable.setValue(4, 0, new Date( 2011, 06, 16 ));
    chartTable.setValue(4, 1, 568.45);
    chartTable.setValue(4, 2, 175.05);
    chartTable.setValue(5, 0, new Date( 2011, 06, 15 ));
    chartTable.setValue(5, 1, 7203.85);
    chartTable.setValue(5, 2, 1343.45);

    var date_formatter = new google.visualization.DateFormat({pattern: 'EEE, MMM-d'});
    date_formatter.format(chartTable, 0); // Apply format to first column of table

    var currency_formatter = new google.visualization.NumberFormat({prefix: '$'});
    currency_formatter.format(chartTable, 1); // Apply format to second column of chart
    currency_formatter.format(chartTable, 2); // Apply format to third column of chart

    // Create and draw the visualization.
    chart = new google.visualization.LineChart(document.getElementById('chart'));
    chart.draw(chartTable, {width: 900, height: 400, title: 'Sales Summary',
        vAxis: {maxValue: 20000, format: '$##,###', viewWindowMode: 'maximized'},
        hAxis: {direction: -1}
    });

是否正确显示除日期之外的所有数据-而不是在图表上显示六月,而是显示七月???同一个月的同几天,但月份不正确?

javascript日期对象从00开始计算月份,因此00=一月,01=二月,等等。。。因此,当您在month字段中使用'06'构建日期时,实际上是第7个月,或者根据wolv2012响应构建的7月,我遇到了这个问题:JS month counting系统是基于零的,而MySQL返回的月数介于1和12之间

在我的日期进入Javascript
新日期(…)
之前,我用SQL对其进行了格式化,我在仍然使用SQL的情况下,通过在我的月数中添加“-1”来补偿这一点:

SELECT DATE_FORMAT(jobs.date, '%Y, %m-1, %d') ...

虽然这看起来有点DIY,但它确实有效。

我也有同样的问题,这条线帮助了我,谢谢!我知道这篇文章很老了,但它与我有关,所以万一将来对别人有帮助:

我对SQL Server的修复是:
选择cast(DATEADD(month,-1,[mydate])作为newdate
,实际上,对于我使用的google注释图表的格式,我必须如下所示:
replace(cast(DATEADD(month,-1,[mydate])as date),“-”,“,”)as dateforJSON

在深入查看date对象时发现!只是让它变得困难!!!现在,每个程序员都知道你从0开始计数;)是的,但是月份的天数从1开始计数,所以有例外!我正在使用date_格式和%m从MySQL获取数据-似乎没有一种方法可以获取我需要的数据-我每个月将有-1个!有没有更好的方法(顺便说一句,把自己归类为“程序员”对我来说是一种延伸):)哈哈,这是一个很好的观点……总是有例外!你是对的,你可能每次抓到这个月的时候都要做a-1。。。我想不出更好的办法了!遇到这种情况是因为C#DateTime.Now.Month在12月份返回12,所以javascript认为已经是明年了。在这个错误上停留的时间太长了。时间旅行有一种让我疲倦的方式。