Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    calculation within form before submit

    Hi Guys!

    There are 3 fields in my form:
    1. Start Date (datetime picker)
    2. End Date (datetime picker)
    3. Amount of Days (textbox)

    I need to calculate the amount of days, based on Start Date input and End Date input.
    Simply to say: Amount of days = End Date - Start Date
    or: field3=field2-field1

    Any suggestions for the respective code? I guess this should be javascript within the form code?
    This calculation needs to be done before submit, so that the field3 is automatically filled in as soon as user picks the Start Date and End Date from the calendar within the form.

    Below is the form code.
    Thanks, Raivis
    Code:
    <div class="ccms_form_element cfdiv_datetime" id="start_date_container_div">
    <label for="start_date">Select Start Date</label>
    <input id="start_date" maxlength="150" size="16" class="cf_date_picker" title="" type="text" value="" name="start_date" />
    
    <div class="clear"></div><div id="error-message-start_date"></div></div>
    
    <div class="ccms_form_element cfdiv_datetime" id="finish_date_container_div">
    <label for="finish_date">Select Finish Date</label>
    <input id="finish_date" maxlength="150" size="16" class="cf_date_picker" title="" type="text" value="" name="finish_date" />
    
    <div class="clear"></div><div id="error-message-finish_date"></div></div>
    
    <div class="ccms_form_element cfdiv_text" id="total_days_container_div">
    <label for="total_days">Total Days</label>
    <input id="total_days" maxlength="150" size="10" class=" validate['required','number']" title="" type="text" value="" name="total_days" />
    
    <div class="clear"></div><div id="error-message-total_days"></div></div>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    you can swap the date strings for the values generated by your date picker:

    Code:
    <script>
    start=new Date ("12 Jun 2012")
    end=new Date ("Aug 13 2012")
    diff=Math.ceil(end.getTime()-start.getTime())/(1000*60*60*24) 
    alert (diff + " days difference")
    </script>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •