JavaScript date.format is not a function

htmljavascript

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?

Best Solution

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