Google Charts显示错误的月份

stU*_*Urb 5 javascript charts google-visualization

我有一个谷歌折线图,显示正确的线; 但是日期的注释完全偏离了一个月.json数据具有正确的日期; 但不知何故谷歌图表转换它: 图表错误的日期

有人知道为什么会这样吗?

Whi*_*Hat 11

没错,正在显示正确的月份

当使用以下日期构造函数时,月份为零 ...

Date(year, month, day, hour, min, sec, mill)

请参阅以下片段...

console.log(new Date(2016,  0, 1)); // <-- Jan
console.log(new Date(2016,  1, 1)); // <-- Feb
console.log(new Date(2016, 11, 1)); // <-- Dec
Run Code Online (Sandbox Code Playgroud)

以下是另一个演示使用json与谷歌图表的片段...

google.charts.load('current', {
  callback: function () {
    var data = new google.visualization.DataTable({
      "cols": [
        {"label": "Date", "type": "date"}
      ],
      "rows": [
        {"c": [{"v": "Date(2016,0,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,1,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,2,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,3,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,4,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,5,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,6,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,7,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,8,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,9,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,10,28,15,0,0)"}]},
        {"c": [{"v": "Date(2016,11,28,15,0,0)"}]},
      ]
    });

    var chart = new google.visualization.Table(document.getElementById('chart_div'));
    chart.draw(data);
  },
  packages:['table']
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
Run Code Online (Sandbox Code Playgroud)

以下是使用上述构造函数创建json日期的php片段

<?php
$date1 = new DateTime();
$date2 = "Date(".date_format($date1, 'Y').", ".((int) date_format($date1, 'm') - 1).", ".date_format($date1, 'd').", ".date_format($date1, 'H').", ".date_format($date1, 'i').", ".date_format($date1, 's').")";

echo $date2;
?>
Run Code Online (Sandbox Code Playgroud)

这是一个PHP小提琴来测试上面的代码片段...

  • 你救了我的一天!从零开始的月份?为什么?....一个反问。这是JavaScript。 (2认同)