Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74158

Simplifying the JavaScript procedure in subtracting values

$
0
0

I will describe the situation from scratch so that there is no clarity in the later stages.

I have Razor/c# code in .cshtml file

it generates table for me

  @for (int nr_rows = 0; nr_rows < 31; nr_rows++)
                {
                    var nr_names = nr_rows + 1;              

                        @for (int nr_columns = 0; nr_columns < 18; nr_columns++)
                        {
                            if (nr_columns == 2)
                            {
                                <td id="td01"><input class="InputsForUser" type="time" name="start_@nr_names" id="id_start_@nr_names" /></td>
                            }
                            if (nr_columns == 3)
                            {
                                <td id="td01"><input class="InputsForUser" type="time" name="end_@nr_names" id="id_end_@nr_names" /></td>
                            }
                            if (nr_columns == 7)
                            {
                                <td id="td01"><input type="time" name="actual_@nr_names" id="id_actual_@nr_names" readonly /></td>
                            }
                        }
                }   

which generates for me html in the browser (interprets in this way)

// ----------------------------------- Day 1<input class="InputsForUser" type="time" name="start_1" id="id_start_1"><input class="InputsForUser" type="time" name="end_1" id="id_end"_1><input type="time" name="actual_1" id="id_actual_1" readonly>

// ----------------------------------- Day 2
<input class="InputsForUser" type="time" name="start_2" id="id_start_2"><input class="InputsForUser" type="time" name="end_2" id="id_end_2"><input type="time" name="actual_2" id="id_actual_2" readonly>

// (...)

// ----------------------------------- Day 31
<input class="InputsForUser" type="time" name="start_31" id="id_start_31"><input class="InputsForUser" type="time" name="end_31" id="id__end_31"><input type="time" name="actual_31" id="id_actual_31" readonly>

I have JavaScript code.

It allows me to subtract id_start_1 from id_end_1 and gives me the result on id_actual_1

and this procedure works

        document.getElementById("id_start_1").onchange = function () {
            diff_1()
        };
        document.getElementById("id_end_1").onchange = function () {
            diff_1()
        };

        function diff_1() {
            start_1 = document.getElementById("id_start_1").value; 
            end_1 = document.getElementById("id_end_1").value; 

            start_1 = start_1.split(":");
            end_1 = end_1.split(":");
            var startDate = new Date(0, 0, 0, start_2[0], start_2[1], 0);
            var endDate = new Date(0, 0, 0, end_2[0], end_2[1], 0);
            var diff = endDate.getTime() - startDate.getTime();
            var hours = Math.floor(diff / 1000 / 60 / 60);
            diff -= hours * 1000 * 60 * 60;
            var minutes = Math.floor(diff / 1000 / 60);



            return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
         }


        setInterval(function () {
             document.getElementById("id_actual_1").value = diff_2();
        }, 1000); 

But this code divides my time into parts and thus subtracts it, I want to make it worse:

I tried:

       document.getElementById("id_start_1").onchange = function () {
            diff_1()
        };
        document.getElementById("id_end_1").onchange = function () {
            diff_1()
        };

        function diff_1() {
             start_1 = document.getElementById("id_start_1").value; 
             end_1 = document.getElementById("id_end_1").value; 

             diff = end_1.getTime() - start_1.getTime();

             return diff;
         }



        setInterval(function () {
                document.getElementById("id_actual_1").value = diff_1();
            }, 1000);

but it doesn't work

" Failed to load resource: the server responnded with a status of 404 () Uncaught TypeError: end_1.getTime is not a function at diff_1"


Viewing all articles
Browse latest Browse all 74158

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>