Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > calculating Num of days

Reply
Thread Tools

calculating Num of days

 
 
riki
Guest
Posts: n/a
 
      11-14-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?))

thnx



 
Reply With Quote
 
 
 
 
Maarten
Guest
Posts: n/a
 
      11-14-2003
Check the source of http://www.westhavenbay.com/english/...kbaarheid.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




 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      11-14-2003
"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
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)


 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      11-14-2003
"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
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      11-14-2003
JRS: In article <pB9tb.4143$(E-Mail Removed)>, 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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      11-14-2003
JRS: In article <EU6tb.23011$(E-Mail Removed)-ops.be>, seen in
news:comp.lang.javascript, Maarten <(E-Mail Removed)> 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)

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
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.
 
Reply With Quote
 
riki
Guest
Posts: n/a
 
      11-17-2003
thanks everyone


 
Reply With Quote
 
riki
Guest
Posts: n/a
 
      11-17-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


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      11-17-2003
JRS: In article <bpal30$kd3$(E-Mail Removed)>, seen in
news:comp.lang.javascript, riki <(E-Mail Removed)> 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).

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      11-17-2003
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

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
from "int cfunc(char *data,int *num) " how can I get the value of'num' in ruby/dl ? bbmerong Ruby 2 01-18-2008 04:38 AM
from "int cfunc(char *data,int *num) " how can I get the value of'num' in ruby/dl bbmerong Ruby 0 01-18-2008 03:14 AM
from "int cfunc(char *data,int *num) " how can I get the value of'num' in ruby/dl bbmerong Ruby 0 01-17-2008 11:41 AM
Calculating days using Business Days for the Calendar David Stockwell Python 2 09-20-2004 09:58 PM
Re: Calculating days using Business Days for the Calendar Gerrit Python 0 09-20-2004 04:39 PM



Advertisments