John Cooper
John Cooper

Reputation: 7651

Cannot find function getMonth in Object 05/06/11

Why do i get this error, when i format my date? Code follows below

var date = /Date(1306348200000)/

function dateToString(date) {
    return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getYear();
}

function dateFromString(str) {
    return new Date(str);
}

Upvotes: 0

Views: 4136

Answers (2)

Felix Kling
Felix Kling

Reputation: 817138

In your code, date is a regular expression, not a Date object. You probably want:

var date = new Date(1306348200000);

Also note that calling date Date without new returns a string and not a Date object.

Edit: Apparently I overlooked the dateFromString function, but your code does not show what you do with date and how you use these functions. Anyway, it should be clear which value you have to pass to Date. Definitely not a regular expression.

Upvotes: 2

zhiyelee
zhiyelee

Reputation: 439

You define var date as a regular which can not be accepted by new Date,just do it like this.

var date = 1312711261103;

try it like this: http://jsfiddle.net/zhiyelee/wLNSS/

Upvotes: 2

Related Questions