16

I am using this piece of code to get string representing date yyyy-mm-dd from a hidden field and then format it as needed:

var date_string = $('#end-date').val();
var splitDate = date_string.split("-");
var end_date = new Date(splitDate[0], splitDate[1] - 1, splitDate[2]);
end_date.format("dddd, mmmm dS, yyyy")

But it throws an error:

end_date.format is not a function

Why does it happen and how to solve this issue?

Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
  • Possible duplicate? http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript –  Feb 03 '12 at 17:39

2 Answers2

22

That is because .format is not a native JavaScript function on Date.prototype.

You need to add a lib like this one: http://jacwright.com/projects/javascript/date_format/

I personally use http://momentjs.com/ to manage dates in JavaScript

Paul
  • 12,392
  • 4
  • 48
  • 58
1

You get this error because Date.prototype.format just does not exist (I am wondering why you think it does).

See this question about solutions how to format dates.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143