c#datetime格式(yyyy-mm-ddT00:00:00)用于谷歌图表日期

时间:2018-05-21 22:53:12

标签: c# .net datetime google-visualization

我即将将.NetCore数据显示到谷歌图表中。日期格式是日期时间。

public List<object[]> GetProdVal()
{
    List<object[]> value = new List<object[]>();
    foreach (var item in valuelist)
    {
        object[] val = {
        item.Date,
        item.TotalCost
        };
        value.Add(val);
    }
    return value;
} 

当我在Google图表中将日期格式视为字符串时,它会返回值并且效果很好。

  prodval_chartData = @Json.Serialize(ViewBag.productionvalue, new Newtonsoft.Json.JsonSerializerSettings {
                StringEscapeHandling =Newtonsoft.Json.StringEscapeHandling.EscapeHtml});

var prodval_data = new google.visualization.DataTable();
            prodval_data.addColumn('date', 'Date');
            prodval_data.addColumn('number', 'Total');
            prodval_data.addRows(prodval_chartData);

但是当我将格式更改为日期时,我收到以下错误:

Uncaught Error: Type mismatch. Value 2018-04-22T00:00:00 does not match type date in column index 0

2 个答案:

答案 0 :(得分:2)

试试这个:

public List<object[]> GetProdVal()
{
    List<object[]> value = new List<object[]>();
    foreach (var item in valuelist)
    {
        object[] val = {
        $"new Date('{item.Date.ToString("yyyy-MM-ddThh:mm:ss")}')",
        item.TotalCost
        };
        value.Add(val);
    }
    return value;
}

答案 1 :(得分:0)

我设法解决了以下问题:

控制器:

    public List<object[]> GetProdVal()
    {
        List<object[]> value = new List<object[]>();
        foreach (var item in valuelist)
        {
            DateTimeConvertor dtc = new DateTimeConvertor();
            double.TryParse(dtc.Year(item.Date.ToString()), out double year);
            double.TryParse(dtc.Month(item.Date.ToString()), out double month);
            double.TryParse(dtc.Day(item.Date.ToString()), out double day);
            object[] val = {
            year,
            month,
            day,                
            item.TotalCost
            };
            value.Add(val);
        }
        return value;
    }

查看:

        prodval_chartData = @Json.Serialize(ViewBag.productionvalue, new Newtonsoft.Json.JsonSerializerSettings {
            StringEscapeHandling =Newtonsoft.Json.StringEscapeHandling.EscapeHtml});

      var prodval_data = new google.visualization.DataTable();
        prodval_data.addColumn('number', 'year');
        prodval_data.addColumn('number', 'month');
        prodval_data.addColumn('number', 'day');
        prodval_data.addColumn('number', 'Total');
        prodval_data.addRows(prodval_chartData);
        //Insert new column for generated date:
        prodval_data.insertColumn(0, 'date', 'Date');
        //Count number of iterations:
        var count = prodval_data.getNumberOfRows();

        for (var i = 0; i < count; i++) {
            //Read year, month, day and calculate date
            var year = prodval_data.getValue(i, 1);
            var month = prodval_data.getValue(i, 2)-1;
            var day = prodval_data.getValue(i, 3);

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

            prodval_data.setValue(i, 0, date);
        }
        // remove columns of year, month, day
        prodval_data.removeColumn(1);
        prodval_data.removeColumn(1);
        prodval_data.removeColumn(1);