Wrong date?

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

    Wrong date?

    function Validaattori_1v iikko(source,ar gs)
    {
    var star_tdate;
    var end_date;
    start_date = document.all("L aina_alku").val ue;
    end_date = document.all("L aina_loppu").va lue;
    var start_day;
    var end_day;
    var start_month;
    var end_month;
    var start_year;
    var end_year;
    start_day = start_date.subs tring(0,2);
    end_day = end_date.substr ing(0,2);
    start_month = start_date.subs tring(3,5);
    end_month = end_date.substr ing(3,5);
    start_year = start_date.subs tring(6,10);
    start_year = end_date.substr ing(6,10);
    var startingdate = new Date();
    startingdate.se tMonth(parseInt (start_month)-1,parseInt(star t_day));
    startingdate.se tYear(parseInt( start_year));

    var endingdate = new Date();
    date_loppu.setM onth(parseInt(e nd_month)-1,parseInt(end_ day));
    date_loppu.setY ear(parseInt(en d_year));

    var dif_indays = Math.round( (endingdate.get Time() -
    startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );

    // For debug
    document.writel n(start_date + " " + end_date);
    document.writel n(startingdate + " " + endingdate);

    if ( (erotus_paivia < 7) && (erotus_paivia > 0) )
    args.IsValid = true;
    else
    args.IsValid = false;
    }

    So Im using this code with ASP.net pages as clientvalidatio n function.
    I don't think that has anything to do with the actual problem though.
    So if i pick start_date of lets say "29.04.2004 " and end_date
    "06.05.2004 " I will get this as debug info:
    29.04.2004 06.05.2004 Thu Apr 29 12:54:05 UTC+0300 2004 Thu May 6
    12:54:05 UTC+0300 2004

    Everything seems to be working just fine, but...

    If i pick end_date of "08.04.2004 " (or "09.05.2004 ") i get this:
    29.04.2004 08.05.2004 Thu Apr 29 12:55:42 UTC+0300 2004 Fri Apr 30
    12:55:42 UTC+0300 2004

    End date is completely wrong.

    Then again if i pick "10.05.2004 " i get:
    ..04.2004 10.05.2004 Thu Apr 29 12:56:36 UTC+0300 2004 Mon May 10
    12:56:36 UTC+0300 2004

    So those 2 dates 8 and 9.5 are giving all wrong dates and Im all out
    of answers. Haven't found that error with any other dates yet.
  • mscir

    #2
    Re: Wrong date?

    Esa wrote:[color=blue]
    > function Validaattori_1v iikko(source,ar gs)
    > {
    > var star_tdate;
    > var end_date;
    > start_date = document.all("L aina_alku").val ue;
    > end_date = document.all("L aina_loppu").va lue;
    > var start_day;
    > var end_day;
    > var start_month;
    > var end_month;
    > var start_year;
    > var end_year;
    > start_day = start_date.subs tring(0,2);
    > end_day = end_date.substr ing(0,2);
    > start_month = start_date.subs tring(3,5);
    > end_month = end_date.substr ing(3,5);
    > start_year = start_date.subs tring(6,10);
    > start_year = end_date.substr ing(6,10);
    > var startingdate = new Date();
    > startingdate.se tMonth(parseInt (start_month)-1,parseInt(star t_day));
    > startingdate.se tYear(parseInt( start_year));
    >
    > var endingdate = new Date();
    > date_loppu.setM onth(parseInt(e nd_month)-1,parseInt(end_ day));
    > date_loppu.setY ear(parseInt(en d_year));
    >
    > var dif_indays = Math.round( (endingdate.get Time() -
    > startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );
    >
    > // For debug
    > document.writel n(start_date + " " + end_date);
    > document.writel n(startingdate + " " + endingdate);
    >
    > if ( (erotus_paivia < 7) && (erotus_paivia > 0) )
    > args.IsValid = true;
    > else
    > args.IsValid = false;
    > }[/color]
    <snip>

    I'd check for typo's first, e.g.:

    var star_tdate; < t after underscore
    start_year = start_date.subs tring(6,10); <start year
    start_year = end_date.substr ing(6,10); <should be end year

    Mike

    Comment

    • mscir

      #3
      Re: Wrong date?

      Esa wrote:
      [color=blue]
      > function Validaattori_1v iikko(source,ar gs)
      > {
      > var star_tdate;
      > var end_date;
      > start_date = document.all("L aina_alku").val ue;
      > end_date = document.all("L aina_loppu").va lue;
      > var start_day;
      > var end_day;
      > var start_month;
      > var end_month;
      > var start_year;
      > var end_year;
      > start_day = start_date.subs tring(0,2);
      > end_day = end_date.substr ing(0,2);
      > start_month = start_date.subs tring(3,5);
      > end_month = end_date.substr ing(3,5);
      > start_year = start_date.subs tring(6,10);
      > start_year = end_date.substr ing(6,10);
      > var startingdate = new Date();
      > startingdate.se tMonth(parseInt (start_month)-1,parseInt(star t_day));
      > startingdate.se tYear(parseInt( start_year));
      >
      > var endingdate = new Date();
      > date_loppu.setM onth(parseInt(e nd_month)-1,parseInt(end_ day));
      > date_loppu.setY ear(parseInt(en d_year));
      >
      > var dif_indays = Math.round( (endingdate.get Time() -
      > startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );
      >
      > // For debug
      > document.writel n(start_date + " " + end_date);
      > document.writel n(startingdate + " " + endingdate);
      >
      > if ( (erotus_paivia < 7) && (erotus_paivia > 0) )
      > args.IsValid = true;
      > else
      > args.IsValid = false;
      > }
      >[/color]
      <snip>

      if i pick start_date of lets say "29.04.2004 "[color=blue]
      > If i pick end_date of "08.04.2004 " (or "09.05.2004 ")
      > End date is completely wrong.[/color]

      This looks like it's working, I used a different method of assigning the
      dates, found on this page:

      JavaScript is a programming language that is primarily used to create interactive and dynamic website content. It can be used to manipulate the Document Object Model (DOM) in a web page, making it a popular choice for creating dynamic user interfaces and web applications.


      var startingdate = new Date(start_year ,start_month-1,start_day);
      var endingdate = new Date(end_year,e nd_month-1,end_day);

      <script type="text/javascript">
      function validate() { //Validaattori_1v iikko(source,ar gs) {
      var
      start_date,end_ date,start_day, end_day,start_m onth,end_month, start_year,end_ year;
      start_date = document.getEle mentById('start date').value;
      end_date = document.getEle mentById('endda te').value;
      start_day = start_date.subs tring(0,2);
      end_day = end_date.substr ing(0,2);
      start_month = start_date.subs tring(3,5);
      end_month = end_date.substr ing(3,5);
      start_year = start_date.subs tring(6,10);
      end_year = end_date.substr ing(6,19);
      var startingdate = new Date(start_year ,start_month-1,start_day);
      var endingdate = new Date(end_year,e nd_month-1,end_day);
      var dif_indays = Math.round( (endingdate.get Time() -
      startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );
      document.getEle mentById('outpu t').innerHTML = 'start date
      '+start_date+'< br>end date '+ end_date+'<br>< br>starting date
      '+startingdate+ '<br>ending date '+ endingdate;
      }
      </script>
      </head>
      <body>
      <input type="text" name="startdate " id="startdate" value="30.01.20 04">
      <input type="text" name="enddate" id="enddate" value="09.05.20 04"
      size="20">
      <input type=button value="doit" onclick="valida te()">
      <div id="output">
      </div>
      </body>
      </html>

      Mike

      Comment

      • Esa Itkonen

        #4
        Re: Wrong date?


        Ah yes, but those typos are there only because I translated the variable
        names from Finnish to English so that everyone can understand them. The
        typos do not excist in my own code.

        Thx for the reply but thats not the problem.


        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Mike

          #5
          Re: Wrong date?

          > var endingdate = new Date();[color=blue]
          > date_loppu.setM onth(parseInt(e nd_month)-1,parseInt(end_ day));
          > date_loppu.setY ear(parseInt(en d_year));
          >[/color]

          I don't know if this is in the real code but "endingdate " is initialized to
          the current system date and never modified. The "date_loppu " object in the
          above code should be "endingdate ".

          Also you may want to try creating the date object by passing in the values
          instead of setting them after the fact.

          Example:

          var endingdate = new Date(parseInt(e nd_year),
          parseInt(end_mo nth)-1,
          parseInt(end_da y),0,0,0,0)


          Comment

          • Grant Wagner

            #6
            Re: Wrong date?

            Esa Itkonen wrote:
            [color=blue]
            > Ah yes, but those typos are there only because I translated the variable
            > names from Finnish to English so that everyone can understand them. The
            > typos do not excist in my own code.
            >
            > Thx for the reply but thats not the problem.[/color]

            You are using parseInt() with only one parameter on values that contain
            leading zeros. As a result, values passed to parseInt() with a leading zero
            are assumed to be base-8. parseInt("08") == 0, parseInt("09") == 0.

            Use the second parameter on parseInt() to specify a base and it will
            properly parse those values. parseInt("08", 10) == 8, parseInt("09", 10) ==
            9.

            --
            | Grant Wagner <gwagner@agrico reunited.com>

            * Client-side Javascript and Netscape 4 DOM Reference available at:
            *


            * Internet Explorer DOM Reference available at:
            *
            Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


            * Netscape 6/7 DOM Reference available at:
            * http://www.mozilla.org/docs/dom/domref/
            * Tips for upgrading JavaScript for Netscape 7 / Mozilla
            * http://www.mozilla.org/docs/web-deve...upgrade_2.html


            Comment

            • Dr John Stockton

              #7
              Re: Wrong date?

              JRS: In article <c419e0f7.04042 90159.15f03086@ posting.google. com>, seen
              in news:comp.lang. javascript, Esa <esa.itkonen@tu runsanomat.fi> posted
              at Thu, 29 Apr 2004 02:59:43 :

              A description of intent would have helped.

              ISTM that alku & loppa contain date strings as dd.mm.yyyy, and you want
              to find the difference between the dates. Consider

              var startingdate =
              new Date(start_date .replace(/(\d+)\D(\d+)\D( \d+)/, "$3/$2/$1"))

              var ending_date = // similar (use a function !)

              Those will accept any all-numeric date in D M Y order with a full year,
              provided that the date is later than B.C. 99. For date validation, see
              via sig below.

              [color=blue]
              > var dif_indays = Math.round( (endingdate.get Time() -
              > startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );[/color]

              Looks OK, but could be Math.round((end ingdate-startingdate)/864e5)

              [color=blue]
              > if ( (erotus_paivia < 7) && (erotus_paivia > 0) )
              > args.IsValid = true;
              > else
              > args.IsValid = false;[/color]

              Could be args.IsValid = erotus_paivia < 7 && erotus_paivia > 0


              Code that is modified for News *MUST* be tested in its modified state.



              Your real problem (which the above would avoid) is almost certainly that
              described in FAQ 4.12; you will get it for the 8th & 9th of every month,
              and for all of August & September. With parseInt, always use a second
              parameter unless you are (a) sure it's not needed, & (b) right. And, if
              the second parameter is 10, you need parseInt only if there may be
              visible characters after the numerics - see FAQ 4.21, and use unary
              plus.

              i.e.
              startingdate.se tYear(parseInt( start_year));
              -> startingdate.se tYear(parseInt( start_year, 10));
              ->> startingdate.se tYear(+start_ye ar);

              --
              © 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> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              • Esa Itkonen

                #8
                Re: Wrong date?

                Sorry fellas for being careless with the code I pasted. Should have
                tested it after translation.

                I got it to work now by adding the parameter 10 to parseInt. Thanks to
                all for help!

                So this function was used in Asp.Net pages as Clientvalidatio nfunction
                for CustomValidator control.
                Don't know if this is the right forum but since its kinda related I'll
                shoot.
                I'v been trying to make a server side Validation for the same thing. I
                have .aspx page and its code is in .aspx.cs-file. Does it matter where
                the OnServerValidat e-function is? In the .aspx-file within "<script
                "runat=serv er>" or just in .aspx.cs-file. I'v read many articles about
                Serverside validation with customvalidator and tried to do as they say
                but it never seems to do anything even if I just put "e.IsValid = false"
                to the check, it still doesn't cause the validation to fail.
                CausesValidatio n-property is "true" in the button I use for submit and
                all the other validators (requiredfield etc...) do work. Does server
                side validation require some setting from IIS? Well hope someone knows
                whats going on :/


                *** Sent via Developersdex http://www.developersdex.com ***
                Don't just participate in USENET...get rewarded for it!

                Comment

                Working...