2

Is it possible to post a DateTime parameter to a Web Method inside an ASMX web service (using a JSON serialized RPC style call)?

I am sending a DateTime to the browser and this gets serialized in the form /Date(1350639464100+0100)/. I can then use the excellent moment.js library to parse the date, display it on the page etc.

My problem is returning this date to the server using an AJAX post to my web service. My web method looks something like this:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void Save(DateTime date)
{
    // Do stuff
}

If I try to send a date in the same format as it came down (/Date(1350639464100+0100)/) then I get an error:

/Date(1350639464100+0100)/ is not a valid value for DateTime.

Are there any better alternatives to sending this up as a string and then parsing the value on the server? Ideally I'd like to be able to send objects on a round trip to and from the server without having to alter any date properties that they may contain.

Thanks for any help!

2 Answers 2

7

I've got this working now as follows.

Web method using a DateTime parameter:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void Save(DateTime date)
{
    // Do stuff
}

AJAX post to the web method sending dates as strings (in an international format to avoid localisation problems):

{"date": "2012-10-19"}

or, using moment.js:

{"date": moment().format("YYYY-MM-DD HH:mm:ss")}
Sign up to request clarification or add additional context in comments.

1 Comment

it is working in Postman too without encapsulating in quotation marks
0

You can change the parameter type to string and then you can convert that string to any type you want

   public void Save(String date){
     *Code*
    }

4 Comments

This is a reasonable fallback option but I'd much rather use a DateTime parameter directly since it'd make it possible to pass the objects to and from the server without having to change any of the properties before sending them up.
The you have to to convert Date(1350639464100+0100) at the client-side to an acceptable date format from server.for example : dd/mm/yyyy or mm/dd/yyyy. Try to send that format like a string... server will recognize it as a date
Thanks for your help Otto, sending the date as a string (but still using a DateTime parameter type) works well. I hadn't thought of trying this until you suggested it, and I was worried about the localisation of this approach. It seems that sending the date in a universal format (e.g. 2012-10-19) is a safe bet.
For clarity I've added another answer (stackoverflow.com/a/12973157/1145963) showing how I've done this without using a string type for the method parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.