Steffen Jørgensen
Steffen Jørgensen

Reputation: 312

Send javascript date to webservice

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

Upvotes: 5

Views: 4765

Answers (2)

danywarner
danywarner

Reputation: 958

Use this: var now = new Date().toISOString();

It will give you the right format (i.e. 2014-02-23T20:16:26:298Z)

Upvotes: 2

Mike Valstar
Mike Valstar

Reputation: 3519

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

Upvotes: 1

Related Questions