最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Send javascript date to webservice - Stack Overflow

matteradmin4PV0评论

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
Add a ment  | 

2 Answers 2

Reset to default 2

Use 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
Post a comment

comment list (0)

  1. No comments so far