Strange mistake at this time of calculating the script?

Basically, this script subtracts StartTime from EndTime using the jQuery plugin, the html form is filled in with the start and end time in the HH: MM format, the input field is filled with the result, it works, except for one problem

If the start time is between 08:00 and 09:59, it simply returns strange results - 10 hours results to be exact, why?

All other inputs are calculated correctly!

function setValue() { var startTime = document.getElementById('ToilA'); var endTime = document.getElementById('EndHours'); startTime = startTime.value.split(":"); var startHour = parseInt(startTime[0]); var startMinutes = parseInt(startTime[1]); endTime = endTime.value.split(":"); var endHour = parseInt(endTime[0]); var endMinutes = parseInt(endTime[1]); //var hours, minutes; var today = new Date(); var time1 = new Date(2000, 01, 01, startHour, startMinutes, 0); var time2 = new Date(2000, 01, 01, endHour, endMinutes, 0); var milliSecs = (time2 - time1); msSecs = (1000); msMins = (msSecs * 60); msHours = (msMins * 60); numHours = Math.floor(milliSecs/msHours); numMins = Math.floor((milliSecs - (numHours * msHours)) / msMins); numSecs = Math.floor((milliSecs - (numHours * msHours) - (numMins * msMins))/ msSecs); numSecs = "0" + numSecs; numMins = "0" + numMins; DateCalc = (numHours + ":" + numMins); document.getElementById('CalculateHours').value = DateCalc; } 
+4
source share
1 answer

Whenever you have math problems with number 8, it turns into an octal system :)

Numbers starting with 0 interpreted as octal numbers in Javascript .

This is not a problem from 01..07 , because they are the same in both systems.

But 08 and 09 do not exist in the system, so they return 0 .

Also see this question , which also provides a solution: specify the base parameter when executing parseInt:

 parseInt("09", 10); // base 10 
+9
source

Source: https://habr.com/ru/post/1314766/


All Articles