admin管理员组文章数量:1434914
I've a .NET webservice which takes an integer and a date object has a parameter. I'm calling the webservice using AJAX with jQuery, but I'm having trouble sending the date in the correct format.
When a .NET webservice returns a date object, it is in the UTC format (milliseconds since 1/1/1970), so I figured I should use the same format when sending a date from the client to the webservice, but when I do (eg. that value Date(1308787200000)
), then I get an error from the webservice saying that it "is not a valid value for DateTime".
What am I missing here?
Regards, Steffen
I've a .NET webservice which takes an integer and a date object has a parameter. I'm calling the webservice using AJAX with jQuery, but I'm having trouble sending the date in the correct format.
When a .NET webservice returns a date object, it is in the UTC format (milliseconds since 1/1/1970), so I figured I should use the same format when sending a date from the client to the webservice, but when I do (eg. that value Date(1308787200000)
), then I get an error from the webservice saying that it "is not a valid value for DateTime".
What am I missing here?
Regards, Steffen
Share Improve this question edited Jun 23, 2011 at 12:11 Adam Hopkinson 28.8k9 gold badges72 silver badges104 bronze badges asked Jun 23, 2011 at 12:04 Steffen JørgensenSteffen Jørgensen 3126 silver badges19 bronze badges 4- "Date(1308787200000)" isn't a valid date, "1308787200000" might be. Not sure why you would put "Date()" around it. – Lazarus Commented Jun 23, 2011 at 12:07
-
when a web service returns a date object it'll be like
Date(1308787200000)
– Jishnu A P Commented Jun 23, 2011 at 12:09 - Mainly because that's what a date looks like when the webservice sends a date back to the client. I just tried to remove Date, leaving just the number-part. Still get the same error :-/ – Steffen Jørgensen Commented Jun 23, 2011 at 12:12
- 1 To passing date through services is not preferable due to the different formats that date can take in at different machine, i prefer to send it as string with specific format and parsing it at the other side – Ahmy Commented Jun 23, 2011 at 12:19
2 Answers
Reset to default 2Use this: var now = new Date().toISOString();
It will give you the right format (i.e. 2014-02-23T20:16:26:298Z)
When you serialize a javascript date object to json it converts to date(time_since_epoch) so that it can easily be converted back with javascript.
.NET on the other hand likes a different format for dates... and that format is dependent on the localization of the machine. Usually this is "mm/dd/yyyy hh:mm:ssAM" in the local timezone of the windows machine.
Your best bet is to either convert this date to a string on the javascript side or to parse the date on the .NET side.
on the .NET side you can convert using the following to convert the string (once again dependent on the local time of the server):
DateTime.Parse
本文标签: jquerySend javascript date to webserviceStack Overflow
版权声明:本文标题:jquery - Send javascript date to webservice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745631536a2667305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论