Trylling
Trylling

Reputation: 213

Passing Date in AngularJS $http get to ASP.NET Web Api

When passing js Date objects to my ASP.NET Web Api controller, I always get null. I have tried passing strings, array of string, timespan - all those works, except Date. When inspecting the request, the date is passed like this:

date:"2014-03-13T15:00:00.000Z"

In angular:

$http({ 
    method: 'get', 
    url: 'api/stuff', 
    params: {
       date: new Date()
    }
);

In my ApiController:

public IEnumerable<StuffResponse> Get(
    [FromUri] DateTime? date
){ ... }

What is the correct way to pass dates?

Upvotes: 6

Views: 9647

Answers (2)

Sergey Kraev
Sergey Kraev

Reputation: 148

This works for me:

$http({ 
    method: 'get', 
    url: 'api/stuff', 
    params: {
       date: $filter('date')(new Date(), "yyyy-MM-dd HH:mm:ss")
    }
);

Upvotes: 13

BenM
BenM

Reputation: 4278

The formatting of the date in your Javascript is not compatible with .NET DateTime parsing. See here for valid formats.

http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

Upvotes: 1

Related Questions