簡體   English   中英

格式化日期字符串以使其適合Google日歷作為參數

[英]Formatting a date string to be suitable for Google calendar as argument

我有一個字符串,代表類似2014年7月2014年的數據。 我正在用javacript格式化該日期,以便可以將其用作Google日歷圖表的參數。

例如

var x = "2014-July-12";

var splitted = x.spilt('-');
// to get "2014" at index [0], "July" at index [1] and "12" at index [2].

然后,我使用鍵值數組來獲取月份數。 然后,我在其中填充Google日歷數據表。

data.addRow([new Date(ParseInt("splitted[0]"),months.splitted[1], ParseInt("splitted[2]")), dataValues[i].Value]);

我使用ParseInt()從字符串轉換為數字,因為新的Date(yyy,mm,dd)只將整數作為參數。 我無法使該日歷正常工作。 我在網上進行了大量搜索,但找不到如何從json文件填充Google日歷日歷圖表的好示例。

你們可以看一下並指導我如何執行此任務,並解釋我錯了嗎。 提前致謝。

繪制日歷圖功能

function drawCalendarChart(jsonObj) {

    var dataValues = eval(jsonObj)
       var data = new google.visualization.DataTable(dataValues);
       data.addColumn({ type: 'date', id: 'Date' });
       data.addColumn({ type: 'number', id: 'Reports' });

       for (var i = 0; i < dataValues.length; i++) {

           var date = new Date(dataValues[i].Date);
           var year = date.getFullYear(), month = (date.getMonth() + 1), day = date.getDate();
           if (month < 10) month = "0" + month;
           if (day < 10) day = "0" + day;
           var Formatted = "" + year + "," + month + "," + day;
           //           data.addRow([new Date(dataValues[i].Date), dataValues[i].Frequencies]);
           data.addRow([new Date(Formatted), dataValues[i].Frequencies]);
       }
       var options = {
         title: "Calendar Chart",
         height: 350
       };
       var chart = new google.visualization.Calendar(document.getElementById('chart'));

       chart.draw(data, options);
       var table = new google.visualization.Table(document.getElementById('table'));
       table.draw(data, { showRowNumber: true });
   }

我添加了我用來繪制圖表的功能。 數據給出了NaN,NaN錯誤。 頻率獲得正確的值。 因此,它必須與日期格式有關。 這是我正在使用的測試字符串。

[
    {
        "Date": "2014-January-15",
        "Frequencies": 11
    },
    {
        "Date": "2014-January-8",
        "Frequencies": 22
    },
    {
        "Date": "2014-January-10",
        "Frequencies": 11
    }
]

保持簡單 ,這應該工作:

data.addRow([ new Date(dataValues[i].Date), dataValues[i].Frequencies ]);

更新

它對我 有用 ,在這里您正在使用代碼。

這是將字符串日期值轉換為編號日期的方法。

var date = new Date("12-January-2014");
var year = date.getFullYear(), month = (date.getMonth() + 1), day = date.getDate();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;

var Formatted = "" + year+"," + month+"," + day;

編輯

  var vt=  new Date(year, month, day);

  alert(vt);

現在,您可以根據需要在數據函數中使用此變量。

這是代碼

希望能幫助到你..!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM