SkyeBoniwell
SkyeBoniwell

Reputation: 7122

converting objects to datetime

I am having a problem converting objects that are dates into actual dates.

I'm reading in values from a datatable, and sending them to a JavaScriptSerializer so I can use the values in an open source calendar called fullCalendar.

The problem is, the feed from the JavaScriptSerializer contains dates and a boolean value.

My code looks like this:

        Dim rows As New List(Of Dictionary(Of String, Object))
    Dim row As Dictionary(Of String, Object)
    For Each dr2 As DataRow In table.Rows
        row = New Dictionary(Of String, Object)
        For Each col As DataColumn In table.Columns
            If TypeOf (dr2(col)) Is DateTime Then
                dr2(col) = Convert.ToDateTime(dr2(col))
            End If
            row.Add(col.ColumnName, dr2(col))
        Next
        rows.Add(row)
    Next

    Dim serializer As New JavaScriptSerializer()
    Dim jsonEvents As String = serializer.Serialize(rows)
    Return jsonEvents

It runs without any errors, and all the values look good EXCEPT the date values. They look strange like this: "\ /Date(1313409600000)\ /". They look like regular dates in the database, so I'm not sure whats going on here.

Any suggestions?

Thanks!

Upvotes: 0

Views: 217

Answers (1)

James Johnson
James Johnson

Reputation: 46077

If you're using .NET 3.5 or above and the columns are DATETIME type, you should be able to do this:

Dim dt As DateTime = dr.Field(Of DateTime)("DateColumn")

Upvotes: 1

Related Questions