Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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

share|improve this answer
    
either use a different format like @Rondles is saying or just take it in as a string and parse it using DateTime.Parse –  SnareHanger Mar 3 '14 at 14:37
    
when sending the js date through postdata, .NET manages to create a DateTime - so I guessed it would work when using get and params also.. –  Trylling Mar 3 '14 at 14:43
    
The format of the date ("2014-03-13T15:00:00.000Z") is exactly the same for $http post data and get params, but the ApiController doesn't manage to recognize the string as a DateTime for get params. –  Trylling Mar 4 '14 at 6:35
    
I would suggest that you try SnareHangers approach and before you send it, convert it into a .NET recognised format. If it still doesn't work then you have a different problem. If it does work then the current formatting combined with this type of AJAX request is causing the issue. –  BenM Mar 4 '14 at 7:06

This works for me:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.