0

I have created the following JSON WCF Service that accepts a .NET System.DateTime value as an input parameter:

[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
ReclaimedH2OMetric GetCurrentReclaimedH2OMetric(System.DateTime currentDate);

When I try to consume the service using jQuery in my web page I get the following error:

The server encountered an error processing the request. The exception message is 'SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.'

Here is my jQuery code:

var rawResults;
var currentDate = new Date('10/1/2012');
var jsonDate = '\\/Date(' + currentDate.getTime() + ')\\/';

$.ajax(
{
    async: false,
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "http://www.mydomain.com/Dashboard_WaterResources/WCFService/Dashboard.svc/GetCurrentReclaimedH2OMetric",
    dataType: "json",
    data: '{"currentDate": "' + jsonDate + '"}',
    success: function (results) {
        rawResults = results;
    },
    error: function (xhr) {
        alert(xhr.responseText);
    }
});

The following line of code var jsonDate = '\\/Date(' + currentDate.getTime() + ')\\/'; was trying to format the date in the proper JSON format using this question as a reference

Community
  • 1
  • 1
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231

2 Answers2

0

The integer inside the jsonDate should be the number of milliseconds since the epoch.

How to convert JavaScript date object to ticks

Or consider using Moment.js to get the milliseconds.

Community
  • 1
  • 1
Rob
  • 5,578
  • 3
  • 23
  • 28
  • I tried the converting the `jsonDate` variable into ticks using the formula provided in the link and still got the same sql overflow error. – Michael Kniskern May 29 '13 at 16:17
  • if you put a breakpoint on the server in the WCF service, what does it say for the date? – Rob May 29 '13 at 16:18
  • currentDate = {1/1/0001 12:00:00 AM} – Michael Kniskern May 29 '13 at 16:21
  • That's DateTime.MinValue, which means the WCF service isn't recognizing the date you're passing in. What happens if you remove the double backslashes, so it's like '\/Date(...)\/'? – Rob May 29 '13 at 16:31
  • I changed the signature of the WCF service to accept a `long` data type instead of `System.DateTime` and it was zero when passed to the service. It look like the input parameter is not recognized during my jQuery ajax call. – Michael Kniskern May 29 '13 at 16:33
  • I think it's the double backslashes, they don't need to be escaped. This is a good article as well: http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx – Rob May 29 '13 at 16:35
0

I modified the WCF service signature to accept a long data type instead of the System.DateTime object.

Also, I was having issues with how I was formatting the data property of the ajax jQuery call. Here is the updated code that worked:

var rawResults;
var currentDate = new Date('10/1/2012 12:00 AM');
var jsonDate = ConvertDateToTicks(currentDate);

$.ajax(
{
    async: false,
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "http://localhost/Dashboard_WaterResources/WCFService/Dashboard.svc/GetCurrentReclaimedH2OMetric",
    data: { dateInTicks: jsonDate },
    success: function (results) {
        rawResults = results;
    },
    error: function (xhr) {
        alert(xhr.responseText);
    }
});
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231