user1166905
user1166905

Reputation: 2622

Problems with outputting date using WebApi/MVC4/Knockout

I am getting dates back in the format: 2012-07-22T:14:45:00

I need to know how to format in JS in order to display in a specific format i.e. 22/07/2012

If I do this:

var date = new Date(self.StartTime());
alert("test " + date.getDate() + "/" + date.getMonth() + "/" + date.getFullYear());

I actually get 22/06/2012 which is a month out??

Upvotes: 1

Views: 301

Answers (2)

BentOnCoding
BentOnCoding

Reputation: 28208

You could write a method on the server side that takes the current value of the date property and formats it into a javascript friendly string and return that from your mvc web api methods. Then you can create javascript date objects from this js friendly date string, because the js date object is cool like that.

For formatting questions see this answer:

Javascript Date() constructor doesn't work

Upvotes: 0

mu is too short
mu is too short

Reputation: 434835

getMonth returns the zero-based month number:

The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

So you need to add one to get the one-based month number you want and then you account for one digit months by manually adding the missing zero. You'll also want to account for single digit getDate values as well.

Upvotes: 2

Related Questions