Why my javascript cant work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wish
    New Member
    • May 2007
    • 65

    Why my javascript cant work?

    Code:
    <html>
    
    <head>
      <title>Testing date function on 8 oct 2007</title>
      <script language="Javascript" src="menu/calender/CalendarPopup.js"></script>
      <script language="Javascript" src="menu/calender/AnchorPosition.js"></script>
      <script language="Javascript" src="menu/calender/date.js"></script>
      <script language="Javascript" src="menu/calender/PopupWindow.js"></script>
    
      <script language="Javascript">
       function calDate(downdate,podate,duration) {
        //alert("You can"+downdate+"You can"+podate+"You can"+duration)
        
        var myDate = new Date()
        var result = myDate.setDate(myDate.getDate()+duration)
        //var myDate = downdate
      
        alert(result)
       }
      </script>
    </head>
    
    <body>
      <form>
    
    <script language="Javascript">
    var cal1 = new CalendarPopup();
    </script>
    
    PO Receive Date:&nbsp;<input type="text" name="date2" value="" size=25>
    <a href="#" onClick="cal1.select(document.forms[0].date2,'anchor2','dd-MMM-yyyy'); return false;" name="anchor2" id="anchor2">Select</a>
    
    <br/>
    
    Downpayment Date:&nbsp;<input type="text" name="date1" value="" size=25>
    <a href="#" onClick="cal1.select(document.forms[0].date1,'anchor1','dd-MMM-yyyy'); return false;" name="anchor1" id="anchor1">Select</a>
    
    <br/>
    
    Estimate Duration:&nbsp;<select name="duration" onChange="calDate(document.forms[0].date1.value,document.forms[0].date2.value,document.forms[0].duration.value)">
                             <option value="">--
                             <option value="1">1
                             <option value="2">2
                             <option value="3">3
                             <option value="4">4
                             <option value="5">5
                             <option value="6">6
                             <option value="7">7
                             <option value="8">8
                             <option value="9">9
                             <option value="10">10
                            </select> Week(s)
    
    <br/>
    
    Estimate Completion Date:&nbsp;<input type="text" name="dateest" value="" size=25>
    
    <br/>
    
    Warranty Date:&nbsp;<input type="text" name="datewarranty" value="" size=25>
    
    <br/>
    
    Shipment Date:&nbsp;<input type="text" name="dateship" value="" size=25>
    <a href="#" onClick="cal1.select(document.forms[0].dateship,'anchorship','dd-MMM-yyyy'); return false;" name="anchorship" id="anchorship">Select</a>
    
      </form>
    </body>
    </html>
    Hi,
    Why my javascript produce funny output to me? Can someone guide me how to calculate the advance date based on the duration from the select box? And the output should pass value in the estimate date input box and not the alert msg box.I use alert msg box for testing purpose.

    Thanks.

    Can someone told me how to use the code tag in this forum?
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    What's wrong happening with you ???

    Debasis Jana

    Comment

    • wish
      New Member
      • May 2007
      • 65

      #3
      Hi,

      I wan to calculate date in advance based on the user choice the date from the downpayment date and duration. I use the javascript to calculate and put the advance date in estimate completion date input box but the javascript cant work. It comes out result with a long integer. Can you guide me?

      Thanks.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by wish
        Hi,

        I wan to calculate date in advance based on the user choice the date from the downpayment date and duration. I use the javascript to calculate and put the advance date in estimate completion date input box but the javascript cant work. It comes out result with a long integer. Can you guide me?

        Thanks.
        Oh ... I see.
        You have the total milliseconds since 1970-1st-January.
        Have a look at this.
        It may help you.
        Have a careful read on it :-)

        Debasis Jana

        Comment

        • wish
          New Member
          • May 2007
          • 65

          #5
          Code:
          function y2k(number) { return ( number<1000 ) ? number+1900:number; }
          
          function add_days(adate,days){
            return new Date (adate.getTime()=(days*86400000));
          }
          
          function format_date(adate){
            return adate.getDate()+'/'+(adate.getMonth()+1)+'/'+y2k(adate.getYear());
          }
          
          function show_dates(adate){
            var then = add_days(now,7);
            var begin = format_date(now);
            var end = format_date(then);
            document.write(begin+'-'+end+'<br>');
          }
          
          var now = new Date(2007,10-1,27);
          
          for (var i=0; i<2; i++){
            show_dates(now);
            now = add_days(now,7);
          }
          the above code will generate the output like this;

          27/10/2007-3/11/2007
          3/11/2007-10/11/2007

          can someone guide me to edit this javascript to make it what i wan?

          1) I would like to pass variable into javascript by the date that selected and duration by user. where should i to edit it?

          2) I wan that user choice the duration (e.g 2 weeks) then pass to generate the date in advance by 2 weeks and call the function to calculate and i dun wan the function auto calculate to me when i haven pass the value in.

          3) the output is 10/11/2007, the less of the output i no need and i would like to put this 10/11/2007 inside the estimate completion date.

          Thanks in advance.

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Have a change this function.

            [code=javascript]
            function add_days(adate, days){
            //return new Date (adate.getTime( )=(days*8640000 0));
            return adate.setDate(a date.getDate()+ days);
            }
            [/code]

            Debasis Jana

            Comment

            • wish
              New Member
              • May 2007
              • 65

              #7
              Hi dmjpro,

              the above change code will effect any thing? I try it but the result is nothing..

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by wish
                Hi dmjpro,

                the above change code will effect any thing? I try it but the result is nothing..
                Post here the total code you tried so far.
                And tell me what's your expected Output and what you are getting now.

                Debasis Jana

                Comment

                • wish
                  New Member
                  • May 2007
                  • 65

                  #9
                  HI,

                  1)I wan pass in the duration from the select box to the for loop so i can calculate the date in advance based on the user select.

                  2)I wan pass in the date from downpayment date to the
                  Code:
                  var now = new Date (2007,10-1,27)
                  instead of default it.

                  3)I only wan this function calculate the date advance when i finish choice the duration in select box.

                  4)The output of this date in advance should print out in the "Estimate Completion Date".

                  Can u guide me the above four items.Thanks.

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    Originally posted by wish
                    HI,

                    1)I wan pass in the duration from the select box to the for loop so i can calculate the date in advance based on the user select.

                    2)I wan pass in the date from downpayment date to the
                    Code:
                    var now = new Date (2007,10-1,27)
                    instead of default it.

                    3)I only wan this function calculate the date advance when i finish choice the duration in select box.

                    4)The output of this date in advance should print out in the "Estimate Completion Date".

                    Can u guide me the above four items.Thanks.
                    [code=javascript]
                    var now = new Date (2007,10-1,27);
                    [/code]

                    This Code is not working?

                    Debasis Jana

                    Comment

                    • wish
                      New Member
                      • May 2007
                      • 65

                      #11
                      Thanks..i already solved.

                      Comment

                      Working...