Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 812
Trylling Avatar asked Mar 03 '14 14:03

Trylling


1 Answers

This works for me:

$http({ 
    method: 'get', 
    url: 'api/stuff', 
    params: {
       date: $filter('date')(new Date(), "yyyy-MM-dd HH:mm:ss")
    }
);
like image 97
Sergey Kraev Avatar answered Nov 02 '22 22:11

Sergey Kraev