This project has moved and is read-only. For the latest updates, please go here.

Jsonconvert.serializeobject creating incorrect date (

Jan 23, 2009 at 5:50 PM
Whenever I try to convert a date property it is always being returned to my callback a day prior to the actual date that the property should be.  Here's a simple test case I created that replicates the behaviour.


<%` Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
    <script src="" type="text/javascript" ></script>
    <script type="text/javascript">
                    type: 'POST',
                    url: 'default.aspx/getDate',
                    data: '[]',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function(json)[
                        var date = eval("("+json.d+")");
                        var pdate = eval("("+json.d.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, 'new $1')+")");
                    error: function(xmlhr, status, err)[
                        var response = eval("("+xmlhr.responseText+")");
    <form id="form1" runat="server">
        <input id="Button1" type="button" value="Get Date" /><br /> <input type="text" id="now" value="" style="width:20em" /><br /> processed: <input type="text" id="pnow" value="" style="width:30em"><br />
        date.strnow: <input type="text" id="strnow" value=""  style="width:20em"/>


Imports System.Web.Services
Imports Newtonsoft.Json

Partial Class _Default
    Inherits System.Web.UI.Page

    Private Class _date
        <JsonProperty()> Public now As New Date
        <JsonProperty()> Public strnow As String
        Public Sub New()
            now = Date.Today
            strnow = Date.Today.ToLongDateString
        End Sub
    End Class

    <WebMethod()> Public Shared Function getDate() As String
        Dim theDate As New _date
        Dim strJson As String

        strJson = JsonConvert.SerializeObject(theDate)

        Return strJson
    End Function
End Class

For example running it today I get the results: /Date(1232694000000-0700)/ processed: Thu Jan 22 2009 23:59:59 GMT-0700 (Mountain Standard Time)
date.strnow: Friday, January 23, 2009

If anyone can see where I'm going wrong it would be greatly appreciated.

Jan 28, 2009 at 6:57 AM
The date is converted to UTC when it is serialized.
Jan 28, 2009 at 5:01 PM
Edited Jan 28, 2009 at 5:08 PM
I figured it had to be something silly that I overlooked, thanks. All I need to do is remove the -0700 portion and it properly converts to local time.