0
votes
// 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.

2
new Date('Tue Mar 09 2010 00:00:00 GMT+0000 (GMT Standard Time)'); works. Check the values passed are not undefined (or anything else for that matter) (alert them inside gDaysBetweenDates)Matt
I suspect that those values are not actually Date instances ...Pointy
How the date string is generated? Do you control its format?Shadow Wizard Wearing Mask V2
It's passed from a 2D array. The value stored in the 2D array is a date object. I've tried everything to get it to MS!Tom Gullen
That date value i posted was from alert(date1)Tom Gullen

2 Answers

1
votes

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.

0
votes

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?)