简体   繁体   English

格式化日期字符串以使其适合Google日历作为参数

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

I have a string that represent a data like 2014-July-2014. 我有一个字符串,代表类似2014年7月2014年的数据。 I am formatting this date in javacript so that I can use it as an argument for Google calendar chart. 我正在用javacript格式化该日期,以便可以将其用作Google日历图表的参数。

Eg 例如

var x = "2014-July-12";

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

I then use a key value array to get the months in number. 然后,我使用键值数组来获取月份数。 Then I populate Google calendar data table with.. 然后,我在其中填充Google日历数据表。

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

I use ParseInt() to convert from string to numbers since new Date(yyy,mm,dd) taks only integers as arguments. 我使用ParseInt()从字符串转换为数字,因为新的Date(yyy,mm,dd)只将整数作为参数。 I cannot get this calendar working. 我无法使该日历正常工作。 I searched a lot on the net but cannot find a good example of how to populate Google calendar calendar chart from json file. 我在网上进行了大量搜索,但找不到如何从json文件填充Google日历日历图表的好示例。

Can you guys take a look and guide me how to do this task and explain were i'm wrong. 你们可以看一下并指导我如何执行此任务,并解释我错了吗。 Thanks in advance. 提前致谢。

Draw Calendar Chart Function 绘制日历图功能

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 });
   }

I added the function i'm using to draw the chart. 我添加了我用来绘制图表的功能。 The data is giving a NaN,NaN error. 数据给出了NaN,NaN错误。 The frequency is getting the right values. 频率获得正确的值。 So it must be related to date formatting. 因此,它必须与日期格式有关。 This is the test string i'm using. 这是我正在使用的测试字符串。

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

Keep it simple , this should work: 保持简单 ,这应该工作:

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

UPDATE 更新

It worked for me , here you have a working fiddle with the code. 它对我 有用 ,在这里您正在使用代码。

Here is how you can convert your string date values to numbered date. 这是将字符串日期值转换为编号日期的方法。

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;

EDIT 编辑

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

  alert(vt);

Now you can use this variables in data function as needed. 现在,您可以根据需要在数据函数中使用此变量。

Here is code 这是代码

Hope it helps..!!! 希望能帮助到你..!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM