Web Develop Wolf
Web Develop Wolf

Reputation: 6346

Returning the DateTime Type as a Date and Not a JSON Timestamp in Web API 2.0

I'm attempting to return a date from an API method in a more friendly format i.e. DD-MM-YYYY, but I only seem to be able to get the JSON timestamp date (0000-00-00T00:00:00).

foreach (var todo in Todos)
{
    todo.ToDoDate = todo.ToDoDate.Date;
}

As you can see in the code I've tried getting round this with the Date function , but this still adds the T00:00:00 extension. I know that I can convert this on the front-end using JavaScript, but I'd prefer not to if I can help it.

Given that todo.ToDoDate is a DateTime type is there any format I can convert this to that will just give me a friendly UK date format?

Upvotes: 1

Views: 1545

Answers (1)

Vidmantas Blazevicius
Vidmantas Blazevicius

Reputation: 4812

Convert it to string then if you want some explicit format:

        var myDate = new DateTime(2017, 11, 13);
        var myDateStr = myDate.ToString("dd/MM/yyyy");
        //myDatestr = "13/11/2017"

Upvotes: 1

Related Questions