Tom Gullen
Tom Gullen

Reputation: 61773

Javascript function undefined

// How many days between two dates
function gDaysBetweenDates(date1, date2) {

    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();


    return Math.round(Math.abs(date1_ms - date2_ms) / (1000 * 60 * 60 * 24));

}

Date1 and date2 being passed are Date objects with values like:

Tue Mar 09 2010 00:00:00 GMT+0000 (GMT Standard Time)

I get a problem when trying to extract the time, it says getTime is undefined. When I try and create a new date object with the dates passed in via:

var dateNew = new Date(date1);

I get the error, date is in invalid format.

Upvotes: 0

Views: 1559

Answers (2)

Jakob
Jakob

Reputation: 24370

For the last problem, run date1.constructor to see what kind of object date1 is. If it is a number is should work as you've written it. If it is a string, you have to make sure that it is formatted as a date. If it's anything but number or string you have an invalid type.

Also, make sure to check that date1 isn't null.

Upvotes: 1

fforw
fforw

Reputation: 5491

You seem to have some type problems. The Date constructor only accepts millisecond Numbers or date strings, not a date object. The other error sounds like you think something is a Date object which is in fact not. (null? a string?)

Upvotes: 0

Related Questions