1

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??

mu is too short
  • 426,620
  • 70
  • 833
  • 800
user1166905
  • 2,612
  • 7
  • 43
  • 75

2 Answers2

2

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
0

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

Community
  • 1
  • 1
BentOnCoding
  • 27,307
  • 14
  • 64
  • 92