Question

I'm looking for a way to pass javascript dates to .NET web api controllers without installing another library on the client...

I'm expecting the javascript dates to deserialize to .NET DateTime.

var date = new Date(); 
post({currentDate: date.toISOString()}); 

Arrives at server as a DateTime.Min (indicating it failed to deserialize).

Here's an example of what is being sent over the wire, but the ApiController is not able to create a DateTime with the correct date...

Request:

{"Date":"2014-04-16T17:03:03.383Z"}

C#:

    [Serializable]
    public class MyObj 
    {
         public DateTime Date { get; set; }
    }

    public class MyController : ApiController
    {
        public HttpResponseMessage Post(MyObj dd)
        {
            // dd's Date property equals DateTime.Min rather than the correct date...
            return null;
        }

    }
}
Was it helpful?

Solution

Remove the [Serializable] attribute.

OTHER TIPS

I think you should push the ticks from the client side. Then the server side will get a date from the ticks. Example:

Javascipt:

var date = new Date(); 
post({ticks: date.getTime()}); 

.NET:

DateTime date = new DateTime(ticks);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top