How to pass a DateTime value to a WebMethod (ASMX)

一世执手 提交于 2019-11-30 12:10:58

Thats because there is specific date/time wire format that ASP.NET Ajax expects - its of form of "\/Date(x)\/", where x is the number of ms elapsed since Jan. 1, 1970 at midnight UTC. So essentially, you need to use some helper function that will convert your JS dates into the needed format while calling the service (and vice versa, date/time json from service to JS date/time object).

So, you have to change code fragment such as

`'", start:/Date('+ start.getTime()+')/, end...` 

to

'", start:"\\\/Date(' + this.getTime() + ')\\\/", end...'

Quickest way to use below plug-in:

http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/

You can find more info in below articles:

http://www.overset.com/2008/07/18/simple-jquery-json-aspnet-webservice-datetime-support/

http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/

http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-full-control-with-the-ajax-function.aspx

This worked for me:

JSON.stringify(new Date())

This converts it into a format like "2014-06-04T14:26:27.129Z", which my web service is happy to accept for a DateTime parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!