1

我有一个 json 数组字符串,我从数据库中获取数据,我需要将 DateAndTime 字段格式化为以下格式:yyyy-MM-dd hh:mm:ss,它需要是动态的作为传递的数据除了 DateAndTime 之外,通过将始终不同

以下是我到目前为止所尝试的。

ASPX

var chartData = <%= DataToJSONChart() %>;
var new_data = []   
for (var i = 0; i < chartData.length; i++) {
   var date = new Date(parseInt(chartData[i].DateAndTime.substr(6)));
   new_data.push(date);
 }
 chartData.tblGeneral = new_data;

VB

Public Function DataToJSONChart() As String
Dim dt As DataTable
Dim ds As New DataSet()
ds = ChartData(4, DateTime.Parse("2012-06-01 00:00:00"), DateTime.Parse("2012-06-10 23:59:59"))
dt = ds.Tables(0)
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
serializer.MaxJsonLength = Int32.MaxValue
Return serializer.Serialize(rows)
End Function
4

1 回答 1

1

您可以通过在服务器端检查其列名来格式化DateTime

For Each dr As DataRow In dt.Rows
    row = New Dictionary(Of String, Object)
    For Each col As DataColumn In dt.Columns
        If col.ColumnName = "DateTimeColumnName" Then
            Dim dt As DateTime = DateTime.Parse(dr(col).ToString())
            row.Add(col.ColumnName, dt.ToString("yyyy-MM-dd hh:mm:ss"))
        Else
            row.Add(col.ColumnName, dr(col))
        End If
    Next
    rows.Add(row)
Next
于 2012-11-25T18:12:33.120 回答