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

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Solutions ArchitectCommented:
The first main problem is that your data property is not passing valid JSON object.
You should pass it as:
data: {
  bookingDate: startTime, 
  durationHours: hours, 
  logonName: logonName

Open in new window

After this is just a matter of seeing if the date format is correct.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itniflProgrammerAuthor Commented:
Yes you were correct, the syntax was incorrect. I ended up with this, which works as long as durationHours is not a decimal number (the receiving method receives integers only),

data: "{bookingDate: "+JSON.stringify(startTime)+", durationHours:"+hours+", logonName:'" +logonName+"'}", 

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.