5

In MVC, you can do something like this:

public ActionResult Index()
{
    return Json(new List<DateTime> { DateTime.Parse("19/02/2017") }, 
                JsonRequestBehavior.AllowGet);
}

And the returned value is ["\/Date(1487458800000)\/"], which is a Javascript Date object.

However, using WebAPI like this:

public IEnumerable<DateTime> Get()
{
    return new List<DateTime> { DateTime.Parse("19/02/2017") };
}

The returned value is <dateTime>2017-02-19T00:00:00</dateTime>

Is there any way that I can serialize a DateTime as a Javascript Date object using WebApi?

6
  • Hi... do you just need the output to be 1487458800000.. I've found a method that uses date subtraction to calculate the Java date based upon the milliseconds elapsed since 1970 Commented Jun 9, 2017 at 11:41
  • I need the object, because I want to bind it to an input datetime-local using Angular. And according to the documentation: "The model must always be a Date object" Commented Jun 9, 2017 at 11:45
  • Martin - Could you create the date objects in script when they are passed in? var date = new Date(1487458800000); Commented Jun 9, 2017 at 11:47
  • The date is inside another object, which is inside a list of thousands. I rather not to convert every date. I know there are ways to convert it from string or ticks, but I'm looking for a way to get this simple task during serialization. MVC does, why Web API not? Commented Jun 9, 2017 at 11:52
  • Maybe you'll need to implement a custom media type formatter: stackoverflow.com/questions/14145097/… Commented Jun 9, 2017 at 12:03

2 Answers 2

0

You can use that format with JavaScript though:

var myDate = new Date("2017-02-19T00:00:00");
Sign up to request clarification or add additional context in comments.

1 Comment

I know. This is not an answer to my question. I dont want to parse the date from a string, I want to convert directly from DateTime.
0

If you change your Accept header on your request to application/json you get:

["2017-06-09T08:14:13.7729697-03:00"]

Try with a tool like Postman.

In angular, you can do something like:

var config = { headers:  {
        'Accept': 'application/json',
    }
};

$http.get("/api/values", config);

7 Comments

Could you explain a little more how to change that header, please. I'm using Angular's $http.get to do the request.
It's kinda hard using a browser. IE defaults to JSON, but it downloads the file. FF and Chrome defaults to XML. Get Postman, then on Headers tab add a key Accept and a value application/json. Change it to application/xml and see how the output changes.
Nevermind, that was a stupid question. In Angular you can do it with headers: { 'Accept': 'application/json' }
Updated the answer with an angular example.
It does not work. Now in Postman I get this: [ "2017-02-19T00:00:00" ]. I need the Date object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.