locked
To find net hrs from the given intime and out time RRS feed

  • Question

  • User-1355965324 posted

    I am looking for the help to find net hrs . I am using the function  called CalculateBreakDownRemainingHrs as given below to find net hrs. Most of the time it is working fine. But when I give the TimeJobStart as 23.3 and  TimeJobFinish  as 1.30 the calculation is going wrong the net hrs should be 2 hrs. but the function gives the result as -22.27 . How can I get the correct time please can you help

    With Thanks

    Pol

     

    model
    public decimal TimeJobStart { get; set; } [DefaultValue(0.00)] public decimal TimeJobFinish { get; set; } public decimal NetHrs { get; set; }
    <div class="form-group">
                        <label for="lblTJS" class="control-label col-sm-1">Time Started</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" id="TimeStart" asp-for="TimeJobStart" onchange="CalculateBreakDownRemainingHrs()"
                                   onkeypress="return (event.charCode >= 48 && event.charCode <= 57) ||event.charCode == 46 || event.charCode == 0 ">
                        </div>
                        <label for="lblTJF" class="control-label col-sm-1">Time Finished</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" id="TimeEnd" asp-for="TimeJobFinish" onchange="CalculateBreakDownRemainingHrs()"
                                   onkeypress="return (event.charCode >= 48 && event.charCode <= 57) ||event.charCode == 46 || event.charCode == 0 ">
                        </div>
                        <label for="lblTJF" class="control-label col-sm-1">Duration</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" asp-for="NetHrs" id="NetHrs" onchange="CalculateBreakDownRemainingHrs()">
                        </div>
                    </div>
    
    
    function CalculateBreakDownRemainingHrs() {
            var fromTime = $('#TimeStart').val().replace('.', ':');
            var toTime = $('#TimeEnd').val().replace('.', ':');
            var fromdate = new Date("05/25/2018 " + fromTime + ":00");
            var todate = new Date("05/25/2018 " + toTime + ":00");
            var msec = todate.getTime() - fromdate.getTime();
            var hh = Math.floor(msec / 1000 / 60 / 60);
            msec -= hh * 1000 * 60 * 60;
            var mm = Math.floor(msec / 1000 / 60);
            msec -= mm * 1000 * 60;
            var ss = Math.floor(msec / 1000);
            msec -= ss * 1000;
            netHrs = hh + "." + mm;
            $("#NetHrs").val(netHrs);
    
    
        }

    Monday, September 23, 2019 3:37 PM

Answers

  • User475983607 posted

    polachan

    I know there is logical error when I compare the same date and hrs are different dates. I am looking for the solution with advised code from the experts how to to find the net hrs  by avoiding the dates to calculate the difference in Hrs.

    I understand that you want the community to write your code.  

    I explained the logical error in your code which should be enough information .  You hard coded the dates but in this edge case the hours span two days.  Your code is doing this...

    [05/25/2018 23:30:00] - [05/25/2018 01:30:00]

    When it should be doing this...

    [05/25/2018 23:30:00] - [05/26/2018 01:30:00]

    You can use an if to test if the start time (23.3) is greater than the end time (1.3).  Then do the proper calculation.  Keep in mind this is not a fool proof solution but it should work if the the hours are under 24.

    I think with this information you should be able to craft a solution.  Looking forward to seeing what you come up with.

            function CalculateBreakDownRemainingHrs() {
                var fromTime = $('#TimeStart').val().replace('.', ':');
                var toTime = $('#TimeEnd').val().replace('.', ':');
                var fromdate = new Date("05/25/2018 " + fromTime + ":00");
                var todate = new Date("05/25/2018 " + toTime + ":00");
    
                if (fromdate > todate) {
                    todate = new Date("05/26/2018 " + toTime + ":00");
                }
    
                var msec = todate.getTime() - fromdate.getTime();
                var hh = Math.floor(msec / 1000 / 60 / 60);
                msec -= hh * 1000 * 60 * 60;
                var mm = Math.floor(msec / 1000 / 60);
                msec -= mm * 1000 * 60;
                var ss = Math.floor(msec / 1000);
                msec -= ss * 1000;
                netHrs = hh + "." + mm;
                $("#NetHrs").val(netHrs);
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2019 7:23 PM

All replies

  • User475983607 posted

    The JavaScript function has hard coded to and from dates (05/25/2018).  This causes a logical bug when trying to calculate hours that from 23:30 to 1:30 the next day.  You should have been able to find this very clear logical error yourself if you were using standard debug tools, dev tools in this case, to troubleshoot your code.  

    Hint: you need an "if".  Looking forward to seeing what you come up with...

    Monday, September 23, 2019 5:35 PM
  • User-1355965324 posted

    I know there is logical error when I compare the same date and hrs are different dates. I am looking for the solution with advised code from the experts how to to find the net hrs  by avoiding the dates to calculate the difference in Hrs.

    Monday, September 23, 2019 6:45 PM
  • User475983607 posted

    polachan

    I know there is logical error when I compare the same date and hrs are different dates. I am looking for the solution with advised code from the experts how to to find the net hrs  by avoiding the dates to calculate the difference in Hrs.

    I understand that you want the community to write your code.  

    I explained the logical error in your code which should be enough information .  You hard coded the dates but in this edge case the hours span two days.  Your code is doing this...

    [05/25/2018 23:30:00] - [05/25/2018 01:30:00]

    When it should be doing this...

    [05/25/2018 23:30:00] - [05/26/2018 01:30:00]

    You can use an if to test if the start time (23.3) is greater than the end time (1.3).  Then do the proper calculation.  Keep in mind this is not a fool proof solution but it should work if the the hours are under 24.

    I think with this information you should be able to craft a solution.  Looking forward to seeing what you come up with.

            function CalculateBreakDownRemainingHrs() {
                var fromTime = $('#TimeStart').val().replace('.', ':');
                var toTime = $('#TimeEnd').val().replace('.', ':');
                var fromdate = new Date("05/25/2018 " + fromTime + ":00");
                var todate = new Date("05/25/2018 " + toTime + ":00");
    
                if (fromdate > todate) {
                    todate = new Date("05/26/2018 " + toTime + ":00");
                }
    
                var msec = todate.getTime() - fromdate.getTime();
                var hh = Math.floor(msec / 1000 / 60 / 60);
                msec -= hh * 1000 * 60 * 60;
                var mm = Math.floor(msec / 1000 / 60);
                msec -= mm * 1000 * 60;
                var ss = Math.floor(msec / 1000);
                msec -= ss * 1000;
                netHrs = hh + "." + mm;
                $("#NetHrs").val(netHrs);
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2019 7:23 PM
  • User-1355965324 posted

    Many Many Thanks

    Monday, September 23, 2019 8:48 PM