We help IT Professionals succeed at work.
Get Started

How to pass a Date() object to a C# WebMethod?

itnifl asked
Last Modified: 2015-11-29
function addBooking(logonName, startTime, endTime) {
    var hours = Math.abs(startTime - endTime) / 36e5;
        type: "POST",
        url: "/Sections/booker.aspx/BookTime",
        data: "{bookingDate= '" + startTime + "', durationHours=" + hours + ", logonName='" + logonName + "'}",
        contentType: "application/json; charset=utf-8;",
        dataType: "json",
        success: function (response) {
            if ($.parseJSON(response.d).runStatus) {
        error: function (xhr, status, error) {
            var err = xhr.responseText;
            alert('Error: ' + err);

Open in new window

Sending to this method:
      public static string BookTime(DateTime bookingDate, int durationHours, string logonName) {

Open in new window

Gives me the error:

"{\"Message\":\"Invalid object passed in, \\u0027:\\u0027 or \\u0027}\\u0027 expected. (13): {bookingDate= \\u0027Mon Nov 30 06:30:00 UTC+0100 2015\\u0027, durationHours=0.5, logonName=\\u0027Admin\\u0027}\",\"StackTrace\":\"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\\r\\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\\r\\n   at System.Web.Script.Services.RestHandler.GetRawParams
(WebServiceMethodData methodData, HttpContext context)\\r\\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)\",\"ExceptionType\":\"System.ArgumentException\"}"

So first I wonder if I am sending over the date object correctly?
Watch Question
Software Architect
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE