这个时间计算脚本有奇怪的错误吗?
基本上,此脚本将从 EndTime 中减去 StartTime,使用 jQuery 插件,html 表单以 HH:MM 格式填充开始和结束时间,输入字段填充结果,除了一个问题外,它可以工作:
如果开始时间为在 08:00 到 09:59 之间,它只是返回奇怪的结果 - 准确地说,结果相差 10 个小时,为什么?
所有其他输入都可以正确计算!
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; }
Basically this script will subtract StartTime from EndTime, using a jQuery plugin the html form is populated with Start and End Time in the format HH:MM, an input field is populated with the result, it works except for one issue:
If Start Time is between 08:00 and 09:59 it just returns strange results - results are 10 hours off to be precise, why?
All other inputs calculate properly!
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; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当您遇到数字 8 的数学问题时,都会将其转换为八进制系统:)
以
0
开头的数字 在 Javascript 中被解释为八进制数字。从
01..07
开始没有问题,因为它们在两个系统中是相同的。但系统中不存在
08
和09
,因此返回0
。另请参阅 此问题,它也提供了解决方案:在执行时指定基本参数解析:
Whenever you have math problems with the number 8, it's something getting converted into the octal system :)
Numbers starting with
0
are interpreted as octal numbers in Javascript.It's no problem from
01..07
, because they are the same in both systems.But
08
and09
don't exist in the system, so they return0
.Also see this question that also provides a solution: Specify the base parameter when doing the parseInt: