从另一个 JavaScript 中减去一次时前导零

发布于 2024-09-08 09:40:15 字数 1207 浏览 6 评论 0原文

function setValue() { 
var startTime = document.getElementById('ToilA'); 
var endTime = document.getElementById('EndHours'); startTime = startTime.value.split(":"); 
var startHour = parseInt(startTime[0], 10); 
var startMinutes = parseInt(startTime[1], 10); 
endTime = endTime.value.split(":"); 
var endHour = parseInt(endTime[0], 10); 
var endMinutes = parseInt(endTime[1], 10); 
//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; }

我对这段代码还有一个问题,当分钟部分超过 10 时,有一个前导 0。

因此它返回类似 11:013 而不是 11:13 的内容,这可以用数学来解决吗?或者 if 语句会解决这个问题吗?就像如果项目数量> 2、删除第一项?

我本来打算在提交表单时编写一个 PHP 脚本来删除它,但它看起来不太好。

function setValue() { 
var startTime = document.getElementById('ToilA'); 
var endTime = document.getElementById('EndHours'); startTime = startTime.value.split(":"); 
var startHour = parseInt(startTime[0], 10); 
var startMinutes = parseInt(startTime[1], 10); 
endTime = endTime.value.split(":"); 
var endHour = parseInt(endTime[0], 10); 
var endMinutes = parseInt(endTime[1], 10); 
//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; }

I have one more issue with this code, there is a leading 0 when the minute part is over 10.

so it return something like 11:013 rather that 11:13, can this be fixed with math or will an if statement fix this? Like if number of items > 2, remove first item?

I was going to just do a PHP script to remove this when the form is submitted, but it doesn't look good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

羁〃客ぐ 2024-09-15 09:40:15

numMins = "0" + numMins 更改为

if (numMins < 10)
    numMins = "0" + numMins;

Change numMins = "0" + numMins to

if (numMins < 10)
    numMins = "0" + numMins;
苍风燃霜 2024-09-15 09:40:15
if (numSecs < 10)
      numSecs = "0" + numSecs;
      if  (numMins < 10)
      numMins = "0" + numMins;
if (numSecs < 10)
      numSecs = "0" + numSecs;
      if  (numMins < 10)
      numMins = "0" + numMins;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文