tags:

views:

70

answers:

3

Hi

How do I get the difference between 2 dates in full days(I don't want any fractions of a day)

var date1 = new Date('7/11/2010');
var date2 = new Date('12/12/2010');
var diffDays = date2.getDate() - date1.getDate(); 
alert(diffDays)

I tried the above but this did not work.

A: 

You can't get the difference between two objects. To do what you want, you need to get a timestamp via Date().getTime();

getDate() simply returns a formatted string, and it doesn't make sense to decrease a string by a string.

So, your code would look like:

var date1 = new Date('7/11/2010');
var date2 = new Date('12/12/2010');
var diffDays = date2.getTime() - date1.getTime(); 
alert(diffDays);

Edit: TNi's code should work like a charm. I'm getting fed up at this keyboard typing race...

Christian Sciberras
Yes, the keyboard race can get annoying very quickly. I've lost a number of those.
TNi
Not true. In javascript, using the subtract operator on a Date will convert the Date to a numeric value (the equivalent of `getTime()`) before performing the operation. `var d2, d1=new Date(); setTimeout(function(){d2=new Date(); alert(d2-d1);}, 100);` alerts `100`.
no
However, doing against a getDate() (as he did) is still wrong, and I would discourage the use of some object-specific "magic".
Christian Sciberras
+3  A: 

Here is one way:

var date1 = new Date("7/11/2010");
var date2 = new Date("12/12/2010");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays)​;

Observe that we need to enclose the date in quotes. The rest of the code gets the time difference in milliseconds and then divides to get the number of days.

TNi
Ah the quotes was a typo. Still I get like 1 day with my way.
chobo2
Whats (1000 * 3600 * 24) what is each of those (mins?seconds? hours?)
chobo2
Another problem that what yours has and @volkan er does not seem to have is if date2 is less than date 1 yours will give a positive number instead of negative.
chobo2
As volkan answered below, the 1000 * 3600 * 24 is the number of milliseconds per day. As for always returning a positive number, that was a feature :) Typically when one talks about the number of days between two dates, that number is positive. If direction matters, just remove the Math.abs().
TNi
Likewise, you get 1 day with your method, because `getDate()` returns the day without regards to the month. Thus, you would get 12 - 11 = 1.
TNi
date already returned time do not need to call .getTime()
volkan er
@volkan er: Indeed, you're right. I can't ever remember things like that.
TNi
A: 
var date1 = new Date("7/11/2010");
var date2 = new Date("8/11/2010");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24)); 

alert(diffDays )
volkan er
What is 1000 * 60 * 60 * 24?
chobo2
date2 - date1 => milliseconds output (1000 * 60 * 60 * 24) => milisecond to day
volkan er
Isn't parseInt completely unnecessary?
Christian Sciberras
to get full value in terms of number of days;request and will be subject to work-related...
volkan er
Christian: he wanted an integral number. parseInt is probably the worst way to do it. Math.round is the 'normal' way; it rounds instead of truncating. If truncating was desired, 'or'ing the expression with 0 would suffice: `(date2 - date1) / (1000 * 60 * 60 * 24) | 0`
no