这个时间计算脚本有奇怪的错误吗?

发布于 2024-09-08 03:13:07 字数 1212 浏览 4 评论 0原文

基本上,此脚本将从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-09-15 03:13:07

每当您遇到数字 8 的数学问题时,都会将其转换为八进制系统:)

0 开头的数字 在 Javascript 中被解释为八进制数字

01..07 开始没有问题,因为它们在两个系统中是相同的。

但系统中不存在0809,因此返回0

另请参阅 此问题,它也提供了解决方案:在执行时指定基本参数解析:

parseInt("09", 10);  // base 10

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 and 09 don't exist in the system, so they return 0.

Also see this question that also provides a solution: Specify the base parameter when doing the parseInt:

parseInt("09", 10);  // base 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文