Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,
I'm working on weekly timesheet mobile application, where the user is displayed his/her weekly timesheet data and based on that person can decide how much more time required to complete the weekly office time.

User is provided with checkbox(in Office), when the checkbox is checked the javascript function periodically runs for every minute.

In this function I want to write logic where it subtracts 1 min from the Avg Hrs Req. say 5:00 and adds 1 minute in the Current Time say 11:41.

Below is my table structure which is displayed to the user

CSS
Curr. Time:     11:41               Last Refresh:       11:41
Avg Hrs      16:30  2:00            6:00 Hrs         16:00  1:30
8:45 Hrs     18:30  3:30            4:30 Hrs         14:30  3:30


Any idea how to write logic for it?
Thanks in avdance
Posted

1 solution

Here is the solution for it

C#
$(document).ready(function () {
   $(document).on('change', '#chkInOffc', function () {

                if ($(this).is(":checked") == true) {
                    alertTimerId = setInterval(function () {
                        resetTime();
                    }, 1000);
                }
                else
                    clearInterval(alertTimerId);
    });
});

function resetTime() {
            var obj1 = $('#tblCT').find('tr');
            var obj2 = $('#tblLR').find('tr');

            var currTime = calTime($(obj1).eq(0).find('td').text(), 1);
            $(obj1).eq(0).find('td').text(currTime);

            var avgTime = calTime($(obj1).eq(1).find('td').eq(1).text(), 0);
            
                if (avgTime.trim() > '00:00')
                    $(obj1).eq(1).find('td').eq(1).text(avgTime);
                else
                    $(obj1).eq(1).find('td').eq(1).text(avgTime).addClass('alert-danger');
                 
            var _845Time = calTime($(obj1).eq(2).find('td').eq(1).text(), 0);
            
                if (_845Time.trim() > '00:00')
                    $(obj1).eq(2).find('td').eq(1).text(_845Time);
                else
                    $(obj1).eq(2).find('td').eq(1).text(_845Time).addClass('alert-danger');

            var _6Time = calTime($(obj2).eq(1).find('td').eq(1).text(), 0);
            
                if (_6Time.trim() > '00:00')
                    $(obj2).eq(1).find('td').eq(1).text(_6Time);
                else
                    $(obj2).eq(1).find('td').eq(1).text(_6Time).addClass('alert-danger');

            var _430Time = calTime($(obj2).eq(2).find('td').eq(1).text(), 0);
            
                if (_430Time.trim() > '00:00')
                    $(obj2).eq(2).find('td').eq(1).text(_430Time);
                else
                    $(obj2).eq(2).find('td').eq(1).text(_430Time).addClass('alert-danger');
            
        }

        function calTime(time, add) {
            
            try{
                var end = time;
                var start = '00:01';

                s = start.split(':');
                e = end.split(':');

                if (add == 0) {
                    
                    min = parseInt(e[1]) - parseInt(s[1]);
                    hour_carry = 0;
                    if (min < 0) {
                        min += 60;
                        hour_carry += 1;
                    }

                    hour = parseInt(e[0]) - parseInt(s[0]) - parseInt(hour_carry);
                    if (min.toString().length == 1) 
                        min = "0" + min;

                    if (hour.toString().length == 1) {
                        hour = "0" + hour;
                    }

                    //alert(min.toString().length);

                    diff = hour + ":" + min;
                    return diff;
                }
                else {
                    
                    min = parseInt(e[1]) + parseInt(s[1]);
                    hour_carry = 0;
                    if (min == 60) {
                        min = 1;
                        hour_carry += 1;
                    }

                    hour = parseInt(e[0]) + parseInt(s[0]) + parseInt(hour_carry);
                    if (min.toString().length == 1)
                        min = "0" + min;

                    if (hour.toString().length == 1)
                        hour = "0" + hour;

                    diff = hour + ":" + min;
                    return diff;
                }
                
            }
            catch(e){
                alert('error');
            }
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900