Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript date.format is not a function

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?

like image 429
Sergei Basharov Avatar asked Feb 03 '12 17:02

Sergei Basharov


1 Answers

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

like image 99
Paul Avatar answered Oct 28 '22 00:10

Paul