Jsonconvert.serializeobject creating incorrect date (vb.net)

Jan 23, 2009 at 4: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.

Default.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script src="http://code.jquery.com/jquery-latest.pack.js" type="text/javascript" ></script>
    <script type="text/javascript">
        $(document).ready(function()[
            $("#Button1").click(function()[
                $.ajax([
                    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')+")");
                        $("#now").val(date.now);
                        $("#pnow").val(pdate.now);
                        $("#strnow").val(date.strnow);
                    ],
                    error: function(xmlhr, status, err)[
                        console.error(arguments);
                        var response = eval("("+xmlhr.responseText+")");
                        console.log(response);
                    ]
                ]);
            ]);
        ]);
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="Get Date" /><br />
        date.now: <input type="text" id="now" value="" style="width:20em" /><br />
        date.now processed: <input type="text" id="pnow" value="" style="width:30em"><br />
        date.strnow: <input type="text" id="strnow" value=""  style="width:20em"/>
    </div>
    </form>
</body>
</html>

Default.aspx.vb

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.now: /Date(1232694000000-0700)/
date.now 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.



Coordinator
Jan 28, 2009 at 5:57 AM
The date is converted to UTC when it is serialized.
Jan 28, 2009 at 4:01 PM
Edited Jan 28, 2009 at 4: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.