Passing Date parametere from Javascript to WCF

Having a service method that accepts two parameters will look like this:

[DataContract()]
public class ServiceRequest
{
[DataMember()]
Nullable<DateTime> DateTimeFrom {
get; set; }
[DataMember()]
Nullable<DateTime> 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 parameters.

      var fromDate = new Date(2013, 06, 18);
var toDate = new Date();

var datavar = {
DateTimeFrom: fromDate.toMSJSON(),
DateTimeTo: toDate.toMSJSON()
};
var parameters = JSON.stringify(datavar);

But, before calling the toMSJSON on date object we need to define it as follows:

// Let Date type be compatible for Microsoft WCF
Date.prototype.toMSJSON = function () {
/// <summary>Let Date type be compatible for Microsoft WCF.
</summary>

var date = ‘/Date(‘ + this.getTime()  + ‘-0000)/’;

};




Getting DateTime from WCF


// convert WCF date to Javascript
String.prototype.DateWCF = function () {
/// <summary>convert string into Date</summary>
var matches = this.match(//Date(([0-9]+)(?:.*))//);
if (matches)
return new Date(parseInt(matches[1]));
};

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.