Rkumar
Rkumar

Reputation: 127

Date.prototype.format is not a function

To format the date, I tried prototyping a method.

Date.prototype.formatYYYYMMDDHHMMSS = function() {
    return (this.getFullYear() + eval(this.getMonth() + 1) 
       + this.getDate() + this.getHours() + this.getMinutes() + this.getSeconds());
};

var upload = Date.now();
var uploadDate = upload.formatYYYYMMDDHHMMSS(); 

But following error is shown:

upload.formatYYYYMMDDHHMMSS is not a function 

Upvotes: 0

Views: 1978

Answers (2)

YaakovHatam
YaakovHatam

Reputation: 2344

Use a date object var upload = new Date();

edit: the ctor Date() default is now.

Upvotes: 0

VisioN
VisioN

Reputation: 145438

That's because Date.now() returns number of milliseconds elapsed since 1 January 1970 00:00:00 UTC and not a Date object.

The correct way of using your approach would be:

var upload = new Date();
var uploadDate = upload.formatYYYYMMDDHHMMSS();

Upvotes: 2

Related Questions