calculating Num of days

R

riki

hello,
i need to calculate num of days between 2 dates...
i get separate parts of dates from html form, then i need to "make" begining
and ending date and calculate difference between them...
something like this, in sybase:
DAYS(YMD(?YEAR?,?MONTH?,?DAY?), YMD(?YEAR1?,?MONTH1?,?DAY1?))

thnx
 
M

Maarten

Check the source of http://www.westhavenbay.com/english/beschikbaarheid.html


function aantalNachten(date1,date2) {
var verschil =

Date.UTC(y2k(date1.getFullYear()),date1.getMonth(),date1.getDate(),0,0,0)
-
Date.UTC(y2k(date2.getFullYear()),date2.getMonth(),date2.getDate(),0,0,0);

nachten = verschil/1000/60/60/24;

if (nachten < 1) nachten = 0;

document.RQ.NACHTEN.value = nachten;

}


aantalNachten = number of nights
result is stored in nachten
 
M

Michael Winter

on 14/11/2003:
hello,
i need to calculate num of days between 2 dates...
i get separate parts of dates from html form, then i need to "make" begining
and ending date and calculate difference between them...
something like this, in sybase:
DAYS(YMD(?YEAR?,?MONTH?,?DAY?), YMD(?YEAR1?,?MONTH1?,?DAY1?))

If you create two Date objects, then subtract one from the other,
you'll get the number of milliseconds between them. For example:

var difference = new Date( 2003, 1, 6 ) - new Date( 2003, 1, 1 );
window.alert( difference / 1000 / 60 / 60 / 24 );

will display 5. If you reversed the order of the subtraction, you'd
get -5.

Mike
 
M

Michael Winter

on 14/11/2003:

Date.UTC(y2k(date1.getFullYear()),date1.getMonth(),date1.getDate(),0,0
,0)

What is the function 'y2k' supposed to do, and what language is it
defined in? It's not native JavaScript or ECMAScript...

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
on 14/11/2003:


If you create two Date objects, then subtract one from the other,
you'll get the number of milliseconds between them.
Correct.

For example:

var difference = new Date( 2003, 1, 6 ) - new Date( 2003, 1, 1 );
window.alert( difference / 1000 / 60 / 60 / 24 );

will display 5. If you reversed the order of the subtraction, you'd
get -5.

Agreed.

But

var difference = new Date( 2003, 3, 23 ) - new Date( 2003, 1, 1 );
window.alert( difference / 1000 / 60 / 60 / 24 );

gives 80.95833333333333 - the explanation can be found via below.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
var verschil =

Date.UTC(y2k(date1.getFullYear()),date1.getMonth(),date1.getDate(),0,0,0)
-
Date.UTC(y2k(date2.getFullYear()),date2.getMonth(),date2.getDate(),0,0,0);

nachten = verschil/1000/60/60/24;

Whatever it is, the function y2k should not AFAICS be needed with
getFullYear(); but, if it is needed, I'd be pleased to see an example.

Date.UTC takes seven arguments. ECMA says that up to 5 can be omitted,
and default to 1 0 0 0 0 - therefore, it is strange to give 6 ending 0 0
0. But not wrong. Netscape says that exactly 4 can be omitted.

In MSIE 4, one can omit any number of arguments.

One can use
Math.round((new Date(2003, 3, 23) - new Date(2003, 1, 1))/864e5)
 
R

riki

hi,
i've tried everything you all proposed me to but can't get good solution to
my problem...
it works fine for 2 dates in the same month, but if starting date is in
different month than ending date, the calculation is incorrect...
for instance 2003.11.30 til 2003.12.02 --> number of days results 3, but it
should result 2 becouse november has 30 and not 31 days...
how should i deal with this problem???
thanks
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
i've tried everything you all proposed me to but can't get good solution to
my problem...
it works fine for 2 dates in the same month, but if starting date is in
different month than ending date, the calculation is incorrect...
for instance 2003.11.30 til 2003.12.02 --> number of days results 3, but it
should result 2 becouse november has 30 and not 31 days...

Actually, there is only one day, 2003-12-01, between those dates; but
they do differ by two days.

See <URL:http://www.merlyn.demon.co.uk/js-date2.htm##DDf>.

You will be aware of the probable need to replace . with / in your
dates.

If you had posted your code, we could have seen the error(s).
 
M

Michael Winter

riki wrote on 17 Nov 2003:
hi,
i've tried everything you all proposed me to but can't get good
solution to my problem...
it works fine for 2 dates in the same month, but if starting
date is in different month than ending date, the calculation is
incorrect... for instance 2003.11.30 til 2003.12.02 --> number
of days results 3, but it should result 2 becouse november has
30 and not 31 days... how should i deal with this problem???
thanks

Were you making those comparisons like so (or an equivalent)?

var diff = new Date( 2003, 12, 2 ) - new Date( 2003, 11, 30 );
window.alert( diff / 86400000 ); // Displays 3

If so, that because the first date evaluates to 2-Jan-2004 and the
second to 30-Dec-2003.

As I very recently discovered, month ordinals are zero based (0 -
Jan, 1 - Feb, etc.) If you change the above to:

var diff = new Date( 2003, 11, 2 ) - new Date( 2003, 10, 30 );
window.alert( diff / 86400000 );

....you'll get 2, as you expect.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top