calculating Num of days

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • riki

    #1

    calculating Num of days

    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?,?MO NTH1?,?DAY1?))

    thnx



  • Maarten

    #2
    Re: calculating Num of days

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


    function aantalNachten(d ate1,date2) {
    var verschil =

    Date.UTC(y2k(da te1.getFullYear ()),date1.getMo nth(),date1.get Date(),0,0,0)
    -
    Date.UTC(y2k(da te2.getFullYear ()),date2.getMo nth(),date2.get Date(),0,0,0);

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

    if (nachten < 1) nachten = 0;

    document.RQ.NAC HTEN.value = nachten;

    }


    aantalNachten = number of nights
    result is stored in nachten




    Comment

    • Michael Winter

      #3
      Re: calculating Num of days

      "riki" wrote on 14/11/2003:
      [color=blue]
      > 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"[/color]
      begining[color=blue]
      > and ending date and calculate difference between them...
      > something like this, in sybase:
      > DAYS(YMD(?YEAR? ,?MONTH?,?DAY?) , YMD(?YEAR1?,?MO NTH1?,?DAY1?))[/color]

      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.u k (remove [no-spam] to reply)


      Comment

      • Michael Winter

        #4
        Re: calculating Num of days

        "Maarten" wrote on 14/11/2003:

        <snip>
        [color=blue]
        >[/color]
        Date.UTC(y2k(da te1.getFullYear ()),date1.getMo nth(),date1.get Date(),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.u k (remove [no-spam] to reply)


        Comment

        • Dr John Stockton

          #5
          Re: calculating Num of days

          JRS: In article <pB9tb.4143$ZT2 .35402980@news-text.cableinet. net>, seen
          in news:comp.lang. javascript, Michael Winter <M.Winter@[no-spam]> posted
          at Fri, 14 Nov 2003 18:58:29 :-[color=blue]
          >"riki" wrote on 14/11/2003:
          >[color=green]
          >> 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"[/color]
          >begining[color=green]
          >> and ending date and calculate difference between them...
          >> something like this, in sybase:
          >> DAYS(YMD(?YEAR? ,?MONTH?,?DAY?) , YMD(?YEAR1?,?MO NTH1?,?DAY1?))[/color]
          >
          >If you create two Date objects, then subtract one from the other,
          >you'll get the number of milliseconds between them.[/color]

          Correct.
          [color=blue]
          > 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.[/color]

          Agreed.

          But

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

          gives 80.958333333333 33 - 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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          • Dr John Stockton

            #6
            Re: calculating Num of days

            JRS: In article <EU6tb.23011$jz 6.666734@phobos .telenet-ops.be>, seen in
            news:comp.lang. javascript, Maarten <nobody@pandora .be> posted at Fri, 14
            Nov 2003 15:54:12 :-
            [color=blue]
            > var verschil =
            >
            >Date.UTC(y2k(d ate1.getFullYea r()),date1.getM onth(),date1.ge tDate(),0,0,0)
            > -
            >Date.UTC(y2k(d ate2.getFullYea r()),date2.getM onth(),date2.ge tDate(),0,0,0);
            >
            > nachten = verschil/1000/60/60/24;[/color]

            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.demo n.co.uk/> - w. FAQish topics, links, acronyms
            PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
            Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

            Comment

            • riki

              #7
              Re: calculating Num of days

              thanks everyone


              Comment

              • riki

                #8
                Re: calculating Num of days

                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


                Comment

                • Dr John Stockton

                  #9
                  Re: calculating Num of days

                  JRS: In article <bpal30$kd3$1@l s219.htnet.hr>, seen in
                  news:comp.lang. javascript, riki <riki_fox@hotma il.com> posted at Mon, 17
                  Nov 2003 15:13:53 :-[color=blue]
                  >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...[/color]

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

                  See <URL:http://www.merlyn.demo n.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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                  <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                  Comment

                  • Michael Winter

                    #10
                    Re: calculating Num of days

                    riki wrote on 17 Nov 2003:
                    [color=blue]
                    > 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
                    >
                    >[/color]

                    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.u k (remove [no-spam] to reply)

                    Comment

                    • riki

                      #11
                      Re: calculating Num of days

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


                      Comment

                      Working...