locked
How to resolve Undefined Value RRS feed

  • Question

  • User-1355965324 posted

    When I calculate the  netHrs the undefined value is coming. 

    TimeStart = 7.30

    TimeEnd = 10

    Then the net hrs should be 2.30. But it is not coming Please help and please advise me the suggested code  to fix my  error in my code

    I am using  the model

    public class BreakDownLogEntryModel
        {
            public decimal TimeJobStart { get; set; }
            public decimal TimeJobFinish { get; set; }
            public decimal NetHrs;
        }
    

    View file

    <div class="row">
                    <div class="form-group">
                        <label for="lblTJS" class="control-label col-sm-1">Time Job Start</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control"  id="TimeStart" asp-for="TimeJobStart" onchange="CalculateBreakDownRemainingHrs()">
                        </div>
                        <label for="lblTJF" class="control-label col-sm-1">Time Job Finish</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" id="TimeEnd" asp-for="TimeJobFinish" onchange="CalculateBreakDownRemainingHrs()">
                        </div>
                        <label for="lblTJF" class="control-label col-sm-1">Hrs</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" asp-for="NetHrs" id="NetHrs">
                        </div>
    
    
                    </div>
                </div>
    My Javascript is as follows
    
    function CalculateBreakDownRemainingHrs() {
            debugger;
            var fromTime = $('#TimeStart').val().replace('.', ':');
            var toTime = $('#TimeEnd').val().replace('.', ':'); 
            var fromdate = new Date("25/05/2018 " + fromTime + ":00");
            var todate = new Date("25/05/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);
            
        }
    

    Wednesday, July 24, 2019 2:12 PM

Answers

  • User-719153870 posted

    Hi polachan,

    The Date object in JS displays the date in the format of "month/day/year" (mm/dd/yyyy).

    In your case, the "25/05/2018" is actually wrong, please change it to "05/25/2018".

    Please refer to below demo based on your code:

    JS:

    <script type="text/javascript">
            function CalculateBreakDownRemainingHrs() {
                debugger;
                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);
    
            }
        </script>

    Here's the result of this demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 25, 2019 2:28 AM

All replies

  • User-1174608757 posted

    Hi  polachan,

    According to your description, I notice that you have defined CalculateBreakDownRemainingHrs() method both for TimeStart and TimeEnd text control.But in your  js code , you didn't judge whether the TimeStart and TimeEnd text both has value or not. So it would cause the problem, for example when TimeStart has got the value of TimeJobStart field ,it will run onchange event automatically,but TimeEnd text hasn't got the value.So it's obvious that toTime value is none and as a result you couldn't get the value of NetHrs.

    I suggest that you could add a logic to check whether both two text has value or not.Here is the code,I hope it could help you.

    js code :

       function CalculateBreakDownRemainingHrs() {
           
            if ($('#TimeStart').val()!= "" && $('#TimeEnd').val()!= "")
            {
            debugger;
            var fromTime = $('#TimeStart').val().replace('.', ':');
            var toTime = $('#TimeEnd').val().replace('.', ':'); 
            var fromdate = new Date("25/05/2018 " + fromTime + ":00");
            var todate = new Date("25/05/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);
               
            }
    
        }

    Best Regards

    Wei

    Thursday, July 25, 2019 2:14 AM
  • User-719153870 posted

    Hi polachan,

    The Date object in JS displays the date in the format of "month/day/year" (mm/dd/yyyy).

    In your case, the "25/05/2018" is actually wrong, please change it to "05/25/2018".

    Please refer to below demo based on your code:

    JS:

    <script type="text/javascript">
            function CalculateBreakDownRemainingHrs() {
                debugger;
                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);
    
            }
        </script>

    Here's the result of this demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 25, 2019 2:28 AM
  • User-1355965324 posted

    Many Many Thanks .  Great help and so many thanks

    Thursday, July 25, 2019 5:37 AM
  • User-1151440187 posted

    hi this issue is occur due to format

    change format of your date to month/date/year (mm/dd/yyyy)

    Wednesday, August 21, 2019 4:34 AM