#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7

    Date range calculation


    Howdy y'all. I almost never touch javascript, so bare with me. The variable I am aiming to print in the third code is a line calculation between 2 textboxes which, as the page loads, will be blank, but then a user will add a date to each one, manually or by calandar selection, and I need this printed variable to update. I found many scripts for date range calculations, but I seem to be missing out on how to make it live. Thanks for any help.

    Code:
                          <script>
                            $(document).ready(function() {
                              $(".datepicker").datepicker();
                            });
                            var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
                            var firstDate = new Date(document.getElementById('start').value);
                            var secondDate = new Date(document.getElementById('end').value);
                            var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
                          </script>
    Code:
                              <TR>
                                <TD><H3>Dates of Absence:</H3></TD>
                                <TD>
                                  <?php
                                    if($action == "add" || $action == "edit") {
                                      echo 'From: <INPUT type="text" name="start" id="start" value="' . $var['start'] . '" class="datepicker" />';
                                    } else {
                                      echo $var['start'];
                                    }
                                  ?>
                                </TD>
                                <TD>
                                  <?php
                                    if($action == "add" || $action == "edit") {
                                      echo 'To: <INPUT type="text" name="end" id="end" value="' . $var['end'] . '" class="datepicker" />';
                                    } else {
                                      echo $var['end'];
                                    }
                                  ?>
                                </TD>
                              </TR>
    Code:
                              <TR>
                                <TD colspan="3">
                                  Paid Off Days: ? of the days in your range are Paid Holidays and will not be charged Vacation Days.
                                </TD>
                              </TR>
                              <TR>
                                <TD colspan="3">
                                  Vacation Days: You will be using <script language="javascript">document.write(diffDays); </script> Day(s) of vacation.
                                </TD>
                              </TR>
    The first line in the final code is to count how many days within this date range are a Holiday, but that is my next step, since I figure I just need subtraction after I figure this main part out.


    EDIT: I guess there are 3 steps to this. :-/
    1.) Calculate full date range. (Not print)
    2.) Count holidays within this range. (Print)
    3.) Subtract Holidays from full count. (Print)
    Last edited by Triple_Nothing; March 22nd, 2013 at 10:48 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Wow. The more thought and attempts I place towards this, the more difficult it becomes. :-/ I just now realized that I have to do the count WITHOUT the weekends. Then, not only that, count Holiday dates within their range that also do not fall on weekends. :-S

    I did get my code live and working, as far as when a user enters their dates, it updates the count lively, but is a full range count at the moment.

    Code:
                            function dateRange() {
                              var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
                              var firstDate = new Date(document.getElementById('start').value);
                              var secondDate = new Date(document.getElementById('end').value);
                              var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
                              document.getElementById("vacDays").innerHTML = diffDays || 0;
                            }
    PHP Code:
    echo 'From: <INPUT type="text" name="start" id="start" value="' $var['start'] . '" onchange="dateRange()" class="datepicker" />';
    echo 
    'To: <INPUT type="text" name="end" id="end" value="' $var['end'] . '" onchange="dateRange()" class="datepicker" />'
    Code:
    Vacation Days: You will be using <SPAN id="vacDays">0</SPAN> Day(s) of vacation.
    Last edited by Triple_Nothing; March 23rd, 2013 at 08:54 AM.

IMN logo majestic logo threadwatch logo seochat tools logo