{"id":26,"date":"2013-06-18T11:37:00","date_gmt":"2013-06-18T11:37:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2013\/06\/18\/passing-date-parametere-from-javascript-to-wcf\/"},"modified":"2013-06-18T11:37:00","modified_gmt":"2013-06-18T11:37:00","slug":"passing-date-parametere-from-javascript-to-wcf","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2013\/06\/18\/passing-date-parametere-from-javascript-to-wcf\/","title":{"rendered":"Passing Date parametere from Javascript to WCF"},"content":{"rendered":"<p>Having a service method that accepts two parameters will look like this:<\/p>\n<pre><span style=\"color: black;\">[DataContract()]<br \/><\/span><span style=\"color: blue;\">public<\/span><span style=\"color: black;\"> <\/span><span style=\"color: blue;\">class<\/span><span style=\"color: black;\"> ServiceRequest<br \/>{<br \/>    [DataMember()]<br \/>    Nullable&lt;DateTime&gt; DateTimeFrom { <\/span><span style=\"color: blue;\">get<\/span><span style=\"color: black;\">; <\/span><span style=\"color: blue;\">set<\/span><span style=\"color: black;\">; }<br \/>    [DataMember()]<br \/>    Nullable&lt;DateTime&gt; DateTimeTo { <\/span><span style=\"color: blue;\">get<\/span><span style=\"color: black;\">; <\/span><span style=\"color: blue;\">set<\/span><span style=\"color: black;\">; }<br \/>}<br \/><br \/>[ServiceContract()]<br \/><\/span><span style=\"color: blue;\">public<\/span><span style=\"color: black;\"> <\/span><span style=\"color: blue;\">interface<\/span><span style=\"color: black;\"> IService<br \/>{<br \/>    [WebInvoke(UriTemplate = <\/span><span style=\"color: maroon;\">\"getServiceCall\"<\/span><span style=\"color: black;\">, Method = <\/span><span style=\"color: maroon;\">\"POST\"<\/span><span style=\"color: black;\">)]<br \/>    ServResponse GetBusinessObjects(ServiceRequest filter);<br \/>}<\/span><\/pre>\n<p>Now, To start calling this method we need to construct a Json data representing the parameters.<\/p>\n<pre>      var fromDate = new Date(2013, 06, 18);<br \/>      var toDate = new Date();<br \/><br \/>      var datavar = {<br \/>            DateTimeFrom: fromDate.toMSJSON(),<br \/>            DateTimeTo: toDate.toMSJSON()<br \/>      };<br \/>      var parameters = JSON.stringify(datavar); <br \/><\/pre>\n<p>But, before calling the <b>toMSJSON<\/b> on date object we need to define it as follows: <\/p>\n<p><\/p>\n<pre><b><b><span style=\"color: green;\">\/\/ Let Date type be compatible for Microsoft WCF<\/span><span style=\"color: black;\"><br \/>Date.prototype.toMSJSON = function () {<br \/><\/span><span style=\"color: green;\">\/\/\/ &lt;summary&gt;<\/span><span style=\"color: green;\">Let Date type be compatible for Microsoft WCF.<\/span><\/b><\/b><b><b><span style=\"color: green;\">&lt;\/summary&gt;<\/span><\/b><\/b><b><b><span style=\"color: green;\"><\/span><\/b><\/b><\/pre>\n<p><span style=\"color: black;\">var date = <\/span><span style=\"color: maroon;\">&#8216;\/Date(&#8216;<\/span><span style=\"color: black;\"> + <\/span><span style=\"color: blue;\">this<\/span><span style=\"color: black;\">.getTime() <\/span><span style=\"color: black;\">&nbsp;+ <\/span><span style=\"color: maroon;\">&#8216;-0000)\/&#8217;<\/span><span style=\"color: black;\">;<\/span><\/p>\n<pre><b><span style=\"color: black;\">};<\/span><\/b><h3><br \/><\/h3><br \/><h3><br \/><b>Getting DateTime from WCF<\/b><\/h3><br \/><b><pre><span style=\"color: green;\">\/\/ convert WCF date to Javascript<\/span><span style=\"color: black;\"><br \/>String.prototype.DateWCF = function () {<br \/>    <\/span><span style=\"color: grey;\">\/\/\/<\/span><span style=\"color: green;\"> <\/span><span style=\"color: grey;\">&lt;summary&gt;<\/span><span style=\"color: green;\">convert string into Date<\/span><span style=\"color: grey;\">&lt;\/summary&gt;<\/span><span style=\"color: green;\"><br \/><\/span><span style=\"color: black;\">    var matches = <\/span><span style=\"color: blue;\">this<\/span><span style=\"color: black;\">.match(\/\/Date(([<\/span><span style=\"color: red;\">0<\/span><span style=\"color: black;\">-<\/span><span style=\"color: red;\">9<\/span><span style=\"color: black;\">]+)(?:.*))<\/span><span style=\"color: green;\">\/\/);<\/span><span style=\"color: black;\"><br \/>    <\/span><span style=\"color: blue;\">if<\/span><span style=\"color: black;\"> (matches)<br \/>        <\/span><span style=\"color: blue;\">return<\/span><span style=\"color: black;\"> <\/span><span style=\"color: blue;\">new<\/span><span style=\"color: black;\"> Date(parseInt(matches[<\/span><span style=\"color: red;\">1<\/span><span style=\"color: black;\">]));<br \/>};<\/span><\/pre>\n<p><\/b><\/p>\n<pre><b>Notice that when you use a date in javascript it is representing your local date based on the settings on your systemn and browser. As a result you might pass different notation to WCF. For example, my broweser has an timezone offset of -120 minutes. So when I ask to convert 06\/20\/2013 02:10:04 I see 2013-06-20T00:10:04.000Z which is perfectly normal because that represents the same exact time as I meant on my browser. Thus WCF will get the correct date aswell.<\/b><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Having a service method that accepts two parameters will look like this: [DataContract()]public class ServiceRequest{ [DataMember()] Nullable&lt;DateTime&gt; DateTimeFrom { get; set; } [DataMember()] Nullable&lt;DateTime&gt; DateTimeTo { get; set; }}[ServiceContract()]public interface IService{ [WebInvoke(UriTemplate = &#8220;getServiceCall&#8221;, Method = &#8220;POST&#8221;)] ServResponse GetBusinessObjects(ServiceRequest filter);} Now, To start calling this method we need to construct a Json data representing the &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2013\/06\/18\/passing-date-parametere-from-javascript-to-wcf\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Passing Date parametere from Javascript to WCF&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19,18,20],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"Having a service method that accepts two parameters will look like this: [DataContract()]public class ServiceRequest{ [DataMember()] Nullable&lt;DateTime&gt; DateTimeFrom { get; set; } [DataMember()] Nullable&lt;DateTime&gt; DateTimeTo { get; set; }}[ServiceContract()]public interface IService{ [WebInvoke(UriTemplate = \"getServiceCall\", Method = \"POST\")] ServResponse GetBusinessObjects(ServiceRequest filter);} Now, To start calling this method we need to construct a Json data representing the&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/26"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}