# calculating Num of days

Discussion in 'Javascript' started by riki, Nov 14, 2003.

1. ### rikiGuest

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

riki, Nov 14, 2003

2. ### MaartenGuest

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

Maarten, Nov 14, 2003

3. ### Michael WinterGuest

"riki" wrote 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

--
Michael Winter

Michael Winter, Nov 14, 2003
4. ### Michael WinterGuest

"Maarten" wrote on 14/11/2003:

<snip>

>

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

--
Michael Winter

Michael Winter, Nov 14, 2003
5. ### Dr John StocktonGuest

JRS: In article <pB9tb.4143\$>, seen
in news:comp.lang.javascript, Michael Winter <M.Winter@[no-spam]> posted
at Fri, 14 Nov 2003 18:58:29 :-
>"riki" wrote 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.

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.

--
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.

Dr John Stockton, Nov 14, 2003
6. ### Dr John StocktonGuest

JRS: In article <EU6tb.23011\$-ops.be>, seen in
news:comp.lang.javascript, Maarten <> posted at Fri, 14
Nov 2003 15:54:12 :-

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

--
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Dr John Stockton, Nov 14, 2003
7. ### rikiGuest

thanks everyone

riki, Nov 17, 2003
8. ### rikiGuest

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

riki, Nov 17, 2003
9. ### Dr John StocktonGuest

JRS: In article <bpal30\$kd3\$>, seen in
news:comp.lang.javascript, riki <> posted at Mon, 17
Nov 2003 15:13:53 :-
>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).

--
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.

Dr John Stockton, Nov 17, 2003
10. ### Michael WinterGuest

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

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

Mike

--
Michael Winter

Michael Winter, Nov 17, 2003
11. ### rikiGuest

aha, so that's the catch...months are zero based.
thank you all very much

riki, Nov 18, 2003