Forum for discussing general topics related to Couch.
2 posts Page 1 of 1
Hi,

I'm using jQuery datepicker with Fullcalendar in an attempt to track employee attendance.
Everithing looks well till now with implementing jQuery datepicker and displaying the days-off for the employees in fullcalendar, but it is required to calculate and sum the employees off days, in order to see how many days-off had a person, how many days-of are remaining, etc.
Here I need your advice.... some guidance.
This is the script for datepicker:
Code: Select all
<script type="text/javascript">
    $(document).ready(function () {
        $('#f_start_date').datepicker({
            dateFormat: 'yy-mm-dd', // Set the desired date format
            onSelect: function (dateText, inst) {
                // Append the desired time to the selected date
                var selectedDateTime = dateText + ' 08:00:00';
                // Set the value of the input field
                $(this).val(selectedDateTime);
            }
        });
    });
</script>

<script type="text/javascript">
    $(document).ready(function () {
        $('#f_end_date').datepicker({
            dateFormat: 'yy-mm-dd', // Displayed date format
            onSelect: function (dateText, inst) {
                // Append the desired time to the selected date
                var selectedDateTime = dateText + ' 15:00:00';
                // Set the formatted date with time as the value of the input field
                $(this).val(selectedDateTime);
            }
        });
    });
</script>

I was thinking of calculating the difference between the 2 dates and than save it into another field.
Code: Select all
<script type="text/javascript">
    $(document).ready(function () {
        $('#f_start_date').datepicker({
            dateFormat: 'yy-mm-dd', // Set the desired date format
            onSelect: function (dateText, inst) {
                calculateAndSaveDifference();
            }
        });

        $('#f_end_date').datepicker({
            dateFormat: 'yy-mm-dd', // Displayed date format
            onSelect: function (dateText, inst) {
                calculateAndSaveDifference();
            }
        });

        function calculateAndSaveDifference() {
            var startDate = $('#f_start_date').datepicker('getDate');
            var endDate = $('#f_end_date').datepicker('getDate');

            // Ensure both dates are selected
            if (startDate && endDate) {
                var millisecondsPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds in a day
                var daysDifference = Math.round((endDate - startDate) / millisecondsPerDay);

                // Calculate weekdays (skipping weekends)
                var weekends = 0;
                for (var i = 0; i <= daysDifference; i++) {
                    var currentDate = new Date(startDate.getTime() + i * millisecondsPerDay);
                    if (currentDate.getDay() === 0 || currentDate.getDay() === 6) {
                        weekends++;
                    }
                }

                var weekdaysDifference = daysDifference - weekends;

                // Set the result in the f_daysoff field
                $('#f_daysoff').val(weekdaysDifference);
            }
        }
    });
</script>

But this is not working. Just like this. I feel I'm missing something here.
Hi,

Saving the number of off-days, skipping weekends, is done.
Here is how I did it, adding the following to the form (<cms:if k_success >) block:
Code: Select all
<cms:php>
    global $CTX;

    // Get the values of the 'start_date' and 'end_date' fields
    $startDate = $CTX->get('frm_start_date');
    $endDate = $CTX->get('frm_end_date');

    // Parse the input dates
    $parsedStartDate = new DateTime($startDate);
    $parsedEndDate = new DateTime($endDate);

    // Initialize a counter for off days
    $offDays = 0;

    // Loop through each day between start and end dates
    $currentDate = clone $parsedStartDate;
    while ($currentDate <= $parsedEndDate) {
        // Check if the current day is a weekend (Saturday or Sunday)
        if ($currentDate->format('N') < 6) {
            $offDays++;
        }

        // Move to the next day
        $currentDate->modify('+1 day');
    }

    // Store the difference in the 'offdays' field
    $CTX->set('offdays', $offDays, 'global');
</cms:php>

<cms:set my_offdays="<cms:show offdays />" 'global' />

                                 <cms:db_persist_form
                                    _invalidate_cache='0'
                                    _auto_title='1'
                                    offdays_number=my_offdays
                              />
2 posts Page 1 of 1