4 Replies Latest reply on Feb 4, 2011 3:23 AM by 835839

    Java/java script auto generate END Date

    833473
      Hi,

      I have a jps page with 4 fields.

      1. drop down list
      2. text box
      3. start date
      4. end date

      the problem is

      text box will allow "number of days"
      start date will get date from user on popup window
      end date has to be generate based on text box value "no of days", another filed which will retrieve from db and start date

      ex:

      if text box values is <50 and start date (user selected date) is 02-04-2010 , the end date should add 8 days from the start date.

      now the problem is calculating end date based on the start date with no of days which is given by user.

      i have spend time to write java script, but i am not able to complete the entire logic.

      please give some idea whether it can perform by java script or java bean.


      Thanks,
      Jasmin Rosline
        • 1. Re: Java/java script auto generate END Date
          gimbal2
          The javascript Date object should be all you need.

          http://www.w3schools.com/js/js_obj_date.asp


          If somehow you cannot figure that out, you can always do an ajax call to a servlet and do the date transformation there. Or even easier (but less user friendly): submit the page on changing the start date / number of days to recalculate the end date server side.
          • 2. Re: Java/java script auto generate END Date
            835839
            now the problem is calculating end date based on the start date with no of days which is given by user
            .

            Use the following code if you want to calculate in client side;
            <html>
            <script>
            function addVal(){
            var d = new Date(document.getElementById('dt').value);
            d.setDate(d.getDate() +eval(document.getElementById('dd').value));
            document.getElementById('result').value = (d.getMonth() +1) +"/"  + d.getDate() + "/" + d.getFullYear(); }
            </script>
            <body>
            <input type="text" id="dt" value="02/03/2011"/><br>
            <input type="text" id="dd"/><br>
            <input type="text" id="result"><br>
            <input type="button" value="add" onclick="addVal()"/>
            </body>
            Edited by: Pandiya on Feb 3, 2011 11:31 PM
            • 3. Re: Java/java script auto generate END Date
              DrClap
              That's brilliant! By the time December comes along and the code breaks, you'll be long gone and there will be nobody to blame but the poor sucker who copied this code.
              • 4. Re: Java/java script auto generate END Date
                835839
                Javascript Date object's getMonth() method return integer from 0 to 11 for the month (From jan to dec).
                Please let me know anything wrong in this. first i will correct myself

                Thanks