2011-08-06 54 views
5

我有一個奇怪的問題,我很困惑與但我在這裏肯定有人會知道我在做什麼錯。我的所有時間都顯示不正確(即六月顯示的是七​​月,7月份顯示月)谷歌圖表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} 
    }); 

是正確顯示的所有數據,除了日期 - 而不是顯示在圖表上6月,正在顯示七月?一個月中的同一天,但這個月是錯誤的?

回答

10

JavaScript的Date對象00開始計數個月,因此00 =月,01月=等等......所以當你使用月字段「06」構建您的日期,它實際上是第7個月,或月

+0

剛剛發現,同時進一步尋找到Date對象!只是爲了讓它難! –

+0

現在,每個程序員知道你從0開始計數;) – wolv2012

+0

是的,但是這個月的日子從1開始計數,所以有例外!我使用DATE_FORMAT和%m獲取MySQL的日期 - 似乎沒有獲得我需要的方法 - 每個月我都會有-1!有沒有更好的方法(順便說一句 - 這對我來說將自己歸類爲「程序員」是一個很好的:) –

2

要建立在wolv2012迴應,我只是碰到了這個問題:JS月計數系統是從零開始的,而MySQL的1到12之間返回月份數

我迄今在SQL格式化它去之前

:JavaScript的 new Date(...),我通過附加「-1」到我的月數,同時仍然在SQL彌補這一

雖然這看起來有點DIY對我來說,它肯定工程。

0

我有同樣的問題,這個線程幫助我,謝謝!我知道這篇文章很老,但它與我有關,所以以防萬一在未來幫助別人:

我的SQL Server修復爲:SELECT cast(DATEADD(month,-1,[mydate]) as newdate而實際上,對於Google註釋圖表格式I用,我有過這樣的格式: replace(cast(DATEADD(month,-1,[mydate]) as date),'-', ',') as dateforJSON